Q:

C Program To Find Average of n Natural Numbers

belongs to collection: Basic C Programming Examples

0

you have to 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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Basic C Programming Examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program to Find Average of n Odd and Even Number... >>
<< C Program to Find Average of Three Numbers...