You are here:   HomeC Programming (Theory)Control Structures in CWrite a C program to find the average of N numbers using do-while () loop.

# Write a C program to find the average of N numbers by using the for loop.

by Dinesh Thakur Category: Control Structures

A program segment to calculate a sum of integers from 1 to n is given in Example. In this example, similar technique is used to determine the sum of n numbers entered from the keyboard. In each iteration of this loop, a number is read from the keyboard in variable n of type float and added to variable sum as shown below.

Note that the body of the loop is a block containing two statements. A complete program to determine the sum and average of n given numbers is given below. Initially, the value of n is read from the keyboard. Then n numbers are accepted from the keyboard and their sum is determined as explained above. Finally, the average of the given numbers (av) is calculated after the for loop and the values of  sum and av are printed.

#include<stdio.h>

#include<conio.h>

void main()

int n,i,sum;

float av;

clrscr();

printf("\n Enter the value of N:");

scanf("%d",&n);

for(i =1;i<=n;i++)

{

sum= sum+i;

}

printf("\n sum is:%d",sum);

av=(float)sum/n;

printf("\n Average is: %f",av);

getche();

}

OUTPUT:

Enter the value of n: 10

sum is: 55

Average is: 5.5

 Name E-mail

Dinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.

What's New and Popular

Popular Article

Search Content

Basic Courses