C Program To Find Average of n Natural Numbers

In this article, we are going to write a c program to find Average of n Natural Numbers.

We will make this program in the following way -:

  • C Program to Find the Average of n Natural Numbers Using For Loop
  • C Program to Find the Average of n Natural Numbers Using While Loop
  • C Program to Find the Average of n Natural Numbers Using Do While Loop
  • C Program to Find the Average of n Natural Numbers Using Function
  • C Program to Find the Average of n Natural Numbers Using Recursion

To make this program, we will use the following concept given below.

If you do not know about these topics well, then you may not understand this program, so you should know about them in detail from the link given above.

Now without wasting time let’s start programming.

C Program To Find Average of n Natural Numbers

C Program to Find Average of n Natural Numbers Using For Loop

Algorithm -:

  • Program Start
  • Variable Declaration (i,n,sum)
  • Input a Number from the User
  • Calculating Sum of n Naturals Numbers
  • Calculating Average of n Naturals Numbers
  • Displaying the Average of n Naturals Numbers
  • Program End

Program -:

//C Program to Find the Average of n Natural Numbers Using For loop

#include<stdio.h>
int main()
{
 //Declaring Variable
  int n, i, sum = 0, count = 0;

//Input Number
 printf("Enter a Number\n");
  scanf("%d",&n);

 for(i=1;i<=n;i++)
  {
      sum = sum + i;
      count++;
  }

  printf("\nAverage of first %d Natural Numbers = %d",n, sum/count);
  return 0;
}

Output -:

Enter a Number
5
Average of first 5 Natural Numbers = 3

C Program to Find the Average of n Natural Numbers Using While Loop

Program -:

//C Program to Find the Average of n Natural Numbers Using While loop

#include<stdio.h>
void main()
{
  int n, i=1, sum = 0,count =0 ;
  printf("Enter a Number\n");
  scanf("%d",&n);

  while(i<=n)
  {
      sum = sum + i;
      count++;
      i++;
  }
  printf("\nAverage of first %d Natural Numbers = %d",n, sum/count);
}

Output -:

Enter a Number 
10

Average of first 10 natural number = 5

C Program to Find the Average of n Natural Numbers Using Do While Loop

Program -:

//C Program to Find the Average of n Natural Numbers Using Do While Loop

#include<stdio.h>
int main()
{
  int n, i=1, sum = 0, count = 0;
  printf("Enter A Number to Calculate Average\n");
  scanf("%d",&n);
do
  {
      sum = sum + i;
      count++;
      i++;
  }while(i<=n);

  printf("\nAverage of first %d Natural Numbers = %d",n,sum/count);

return 0;
}

Output -:

Enter a number to calculate average
10

Average of first 10 Natural Number = 5

C Program to Find the Average of n Natural Numbers Using Function

Program -:

//C Program to Find the Average of n Natural Numbers Using function

#include<stdio.h>
int average(int);
void main()
{
  int n, i=1, avg;

 printf("Enter A Number\n");
  scanf("%d",&n);

  avg=average(n);
  printf("\nAverage of first %d Natural Numbers = %d",n,avg);
}
int average(int n)
{
    int i,sum=0,count =0;
    for(i=1;i<=n;i++)
    {
        sum = sum + i;
        count++;
    }
    return (sum/count);
}

Output -:

Enter a number
20

Average of first 20 Natural  Numbers = 10

C Program to Find the Average of n Natural Numbers Using Recursion

Program -:

//C Program to Find the Average of n Natural Numbers Using Recursion

#include<stdio.h>
int sum(int);
int average(int, int);
void main()
{
  int n, s,avg;

 printf("Enter A Number\n");
  scanf("%d",&n);

  s=sum(n);
  avg = average(s, n);
  printf("\nAverage of first %d Natural Numbers = %d",n,avg);
}
int sum(int n)
{
    int s=0;

    if(n==1)
        return (n);

    s = n + sum(n-1);
     return (s);
}
int average(int s, int n)
{
    return (s/n);
}

Output -:

Enter a Number
100

Average of first 100 Natural Numbers = 50

Conclusion

So friends, in this article, we have write a program to calculate the Average n natural numbers in five different ways.

  • C Program to Find the Average of n Natural Numbers Using For Loop
  • C Program to Find the Average of n Natural Numbers Using While Loop
  • C Program to Find the Average of n Natural Numbers Using Do While Loop
  • C Program to Find the Average of n Natural Numbers Using Function
  • C Program to Find the Average of n Natural Numbers Using Recursion

If you have difficulty to understand any program or have any doubts or questions, then tell me in the comment below.

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging

Leave a Comment