To find the average of first n natural numbers
Program:
#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();
}
Output:
Enter the limit : 5
Average of first 5 numbers is : 3.00
-
UpdatedDec 30, 2019
-
Views20,799
You May Like
Program to maintain employee details using structures
Program to maintain student details using structures
Check whether the person is eligible to vote or not
Print the numbers that are divisible by a given no.
Program to generate magic square
To sort the given numbers in ascending & descending order