Thursday 12 March 2015

To find the average of first n natural numbers Using C


# include <stdio.h>
# include <conio.h>
void main()
{
int n, i ;
float sum = 0, avg ;
clrscr() ;
printf("Enter the limit : ") ;
scanf("%d", &n) ;
for(i = 1 ; i <= n ; i++)
sum = sum + i ;
avg = sum / n ;
printf("\nAverage of first %d numbers is : %.2f", n, avg) ;
getch() ;
}

RUN 1 :
~~~~~~~
Enter the limit : 5
Average of first 5 numbers is : 3.00

No comments:

Post a Comment