c program for Average of n numbers

In this article, we are going to discuss how to calculate the average of n numbers. So let’s start!

C program to find Average of n numbers

For example –

Input :

So to calculate the average of n numbers we are going to take user input so we take a number from the user. So for example we take 5 numbers from the user,

logic:

Before going to the program we first know about some basic concepts which going to use in our program such as

1. the looping concept.
2. array in c.

for(i=0; i<n; i++)
printf(“Enter the number %d: “, i+1);
scanf(“%f”, &num[i]);
sum+= num[i];

Output

After giving 5 inputs we get this type of output:
Enter the number of elements:5
Enter the number 1: 22
Enter the number 2: 33
Enter the number 3: 1
Enter the number 4: 4
Enter the number 5: 5
The average number is 13.000000

If these two concepts are clear to you and you know how to use then this is easy to understand this program. Basically, there are two types of examples we are going to discuss in our program. The first is by taking user input and another one is without using user input. So let’s discuss these examples one by one.

Example 1: by taking user input.

// C program to find the average of n numbers :

for(i=0; i<n; i++){
printf(“Enter the number %d: “, i+1);
scanf(“%f”, &num[i]);
sum+= num[i];
}
avg =sum / n;
printf(“Average of the number is %f”, avg);
return 0;

}

Output

Enter the number of elements:5
Enter the number 1: 22
Enter the number 2: 33
Enter the number 3: 1
Enter the number 4: 4
Enter the number 5: 5
The average number is 13.000000

Step 1: First we are taking to int type variable one is for user input and the second one is for a loop. After that, we get three float-type variables. The first is to create an array, the second is to calculate the average of numbers and the third is to calculate the sum of numbers.

Step 2: In the next step, we can see that in our program we are using for loop to take input from the user. And apply that for loop so that the user can take the n number that they want.

Step 3: After taking user input we use the sum variable to calculate the sum of the total number.

Step 4: then after applying the loop we use an average formula to calculate the average of all number that is taken by the user.

Step 5: after all these steps we simply print the average to take our final output.

In this way, we simply understand how we can find out the average of n numbers in the c program.