# C Programming language

C Program example: Print average of n number
 Previous Home Next
Pint average of n number

Here we are using two header files and five predefine methods. In for loop we sum from 1 to n and then finally find average. In this we are using "\t" for 5 white space.

In this we first declare two integer variable(i,n) ,one float variable(avg) and initialize an integer variable(sum=0). Then to clear the screen we call clrscr() function. After that pint a message .To insert value from keyboard we are using scanf() function. To calculate sum start for loop from 1 to n.

In first iteration calculate sum(0)+i(i=1) and assign into sum=sum+1.Then In first iteration add sum=sum(sum=1)+i(i=2) similarly up to n. After completing for loop calculate average (avg=(float)sum/n). Here we are using (float)sum/n) to calculate float value of average. Finally print average as output.

```/* print average of n number. */

#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0,n;
float avg;
clrscr();
printf("Enter a to which you have to
calculate the arg:\t");
scanf("%d",&n);
for(i=1;i<=n;i++)
sum=sum+i;
avg=(float)sum/n;
printf("The average:\t%f",avg);
getch();
}
```
Output :

Enter a to which you have to calculate the arg: 12

The average: 6.500000

 Previous Home Next