arithmetic mean – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Fri, 26 Aug 2022 07:33:27 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg arithmetic mean – Programmerbay https://programmerbay.com 32 32 C Program to Calculate Average, Geometric and Harmonic Mean https://programmerbay.com/c-program-that-calculates-the-average-geometric-and-harmonic-mean/ https://programmerbay.com/c-program-that-calculates-the-average-geometric-and-harmonic-mean/#respond Thu, 25 Aug 2022 07:05:22 +0000 https://programmerbay.com/?p=5357 The C programs calculate geometric mean, average mean, and harmonic mean using their respective formulas. It simply applies these formula on the given input to calculate and print the result.

In this, we are using following formula directly in the code:

Geometric Mean :Geometric meanAverage : arthimatic meanHarmonic mean :Harmonic mean

Example 1. Program to calculate arithmetic mean in C

Program:

#include<stdio.h>


int main ()
{
  int i, size;
  float arr[10], arithmeticMean;
  float sum = 0;
  printf ("Please enter the number of elements = ");
  scanf ("%d", &size);
  printf ("Now, enter the elements \n");
  for (i = 0; i < size; i++)
    {
      printf ("Element %d : ", i + 1);
      scanf ("%f", &arr[i]);
    }
    
  for (i = 0; i < size; i++)
    {
      sum = sum + arr[i];
    }
    
  arithmeticMean = (float) (sum / size);

  printf
    ("\n Average or Arithmetic Mean = %.2f", arithmeticMean);

  return 0;
}

Output

Please enter the number of elements = 9
Now, enter the elements 
Element 1 : 1
Element 2 : 2
Element 3 : 3
Element 4 : 4 
Element 5 : 5
Element 6 : 6
Element 7 : 7
Element 8 : 8
Element 9 : 9

 Average or Arithmetic Mean = 5.00

Example 2. Program to calculate Harmonic mean in C

Program:

#include<stdio.h>


int main (){
  int i, size;
  float arr[10], oneNthValue = 0;
  float harmonicMean;
  printf ("Please enter the number of elements = ");
  scanf ("%d", &size);
  printf ("Now, enter the elements \n");
  for (i = 0; i < size; i++) {
      printf ("Element %d : ", i + 1);
      scanf ("%f", &arr[i]);
    }

  for (i = 0; i < size; i++) {
      oneNthValue = oneNthValue + 1 / arr[i];
    }

  harmonicMean = (float) size / oneNthValue;

  printf ("\n Harmonic mean= %.2f ", harmonicMean);

  return 0;
}

Output:

Please enter the number of elements = 5
Now, enter the elements 
Element 1 : 2
Element 2 : 4
Element 3 : 6
Element 4 : 8
Element 5 : 10

 Harmonic mean= 4.38

Example 3. Program to calculate Geometric mean in C

Program: 

#include<stdio.h>
#include<math.h>


int main ()
{
  int i, size;
  double product = 1, root;
  float arr[10], oneNthValue = 0;
  float geometricMean;
  printf ("Please enter the number of elements = ");
  scanf ("%d", &size);
  printf ("Now, enter the elements \n");
  for (i = 0; i < size; i++)
    {
      printf ("Element %d : ", i + 1);
      scanf ("%f", &arr[i]);
    }
    
  for (i = 0; i < size; i++)
    {
      product = (double) product *arr[i];
    }
    
  root = (double) 1 / size;
  geometricMean = (float) pow (product, root);

  printf("Geometric mean = %.2f", geometricMean);

  return 0;
}

Output:

Please enter the number of elements = 5
Now, enter the elements 
Element 1 : 12
Element 2 : 23
Element 3 : 14
Element 4 : 11
Element 5 : 17
Geometric mean = 14.85

Write a program that reads a series of n numbers and calculates the average, geometric and harmonic mean of n elements in an array in C 

Program:

#include<stdio.h>
#include<math.h>

/*
C program to calculate arithmetic mean, harmonic mean and geometric mean

*/
int main ()
{
  int i, size;
  double product = 1, root;
  float arr[10], oneNthValue = 0;
  float harmonicMean, averageMean, geometricMean;
  float sum = 0;
  printf ("Please enter the number of elements = ");
  scanf ("%d", &size);
  printf ("Now, enter the elements \n");
  for (i = 0; i < size; i++)
    {
      printf ("Element %d : ", i + 1);
      scanf ("%f", &arr[i]);
    }
    
  for (i = 0; i < size; i++)
    {
      oneNthValue = oneNthValue + 1 / arr[i];
      sum = sum + arr[i];
      product = (double) product *arr[i];
    }
    
  root = (double) 1 / size;
  harmonicMean = (float) size / oneNthValue;
  averageMean = (float) (sum / size);
  geometricMean = (float) pow (product, root);

  printf
    ("\n Harmonic mean= %.2f \n Arithmetic mean = %.2f \n Geometric mean = %.2f",
     harmonicMean, averageMean, geometricMean);

  return 0;
}

 

Output:

Please enter the number of elements = 5 
Now, enter the elements 
Element 1 : 21 
Element 2 : 3 
Element 3 : 45 
Element 4 : 76 
Element 5 : 89 
Harmonic mean= 11.69 
Arithmetic mean = 46.80 
Geometric mean = 28.61

Explanation:

  1. Getting input array  from a user
  2. Then, applying formula on the given set of elements , that would output average, geometric and harmonic mean

]]>
https://programmerbay.com/c-program-that-calculates-the-average-geometric-and-harmonic-mean/feed/ 0