Home / Programs / Write a program that asks the user to enter some numbers and then find their average using counter-based loop.
Programming Example

Write a program that asks the user to enter some numbers and then find their average using counter-based loop.

👁 1,041 Views
💻 Practical Program
📘 Step by Step Learning
Write a program that asks the user to enter some numbers and then find their average using counter-based loop.

Program Code

#include <stdio.h>
int main()
{
int n, a, c=1,s=0;
float avg;
printf("\n HOW MANY NUMBERS?");
scanf("%d", &n);

while(c<=n)
{
 printf("\n Enter the number: ");
 scanf("%d", &a);
 s+=a;
 c++;
}
 avg=(float)s/n;
 printf(" \n AVERAGE IS %f ", avg);

getch();
return 0;
}

Output

<b>Output 1:</b>

 HOW MANY NUMBERS?5

 Enter the number: 1

 Enter the number: 2

 Enter the number: 3

 Enter the number: 4

 Enter the number: 5

 AVERAGE IS 3.000000

Explanation

None

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.