Home » C Programs » C Program to Find Average of n Numbers

C Program to Find Average of n Numbers

Basic program to c program to find average of n numbers  using this program we can calculate average in c with help of for loop and while in a single program for beginners to understand concepts in c programming.

For Loop in C

A for loop is a repetition control structure that allows us to create a loop that runs a set number of times. The loop allows us to do an arbitrary number of steps in a single line. A loop is used to repeatedly execute a set of statements until a certain condition is met. Here we have executed this calculate the average of given numbers in c using looping statement.

While Loop in C

A “While” Loop is used to repeat a block of code an unknown number of times until it meets a condition. While a while loop examines the condition before executing the statement(s), a do while loop will execute the statement(s) at least once before checking the condition.

Return Statement to Calculate Average in C

A return statement marks the end of a function’s execution and hands control back to the calling function. The calling function continues execution at the place where the call was made. A return statement can give the caller function a value. When a return statement is used in the body of a function, the function’s execution is halted. A supplied value is returned to the function caller if it is specified. 

C Program to Find Average of n Numbers

//C Program to Find Average of n Numbers
#include <stdio.h>
int main() 
{
int n, i;
float num[100], sum = 0.0, avg;
printf("Enter Numbers");
scanf("%d", &n);
while (n > 100 || n < 1) 
{
printf("Error Not Found\n");
printf("Enter Number Again ");
scanf("%d", &n);
}
for (i = 0; i < n; ++i) 
{
printf("%d. Enter number", i + 1);
scanf("%f", &num[i]);
sum += num[i];
}
avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}

Output:

Enter Numbers3
1. Enter number45
2. Enter number67
3. Enter number89
Average = 67.00