C Program to Find the Sum and Average of n Numbers

In this article, we are going to write a c program to find the sum and average of n numbers.

We will make this program in the following way -:

  • C Program to Find the Sum and Average of n Numbers (Simple Way)
  • C Program to Find the Sum and Average of n Numbers Using Function
  • C Program to Find the Sum and Average of n Numbers Using while Loop
  • C Program to Find the Sum and Average of n Numbers Using Do while Loop
  • C Program to Find the Sum and Average of n Numbers Using Array

Let’s understand all these methods one by one.

C Program to Find the Sum and Average of n Numbers

C Program to Find the Sum and Average of n Numbers (Simple Way)

To make this program, you must first know about the loop because we are going to use the concept of loop to make this program.

There are mainly three types of loops – : While Loop, Do While Loop, and For Loop.

You can read about loop in detail here -:

Here we are going to make this program with the help of these three types of loops.

Let’s first make this program with the help of simple loop.

Algorithm

  • Program Start
  • Declaring Variables (i,n,sum, num)
  • Input a Numbers from User
  • Calculating Sum of n Numbers
  • Calculating Average of n Numbers
  • Displaying the Sum of n Numbers
  • Displaying the Average of n Numbers
  • Program End

Program to Calculate Sum and Average of n Numbers In C

//Program to Calculate Sum and Average of n Numbers In C Using Loop

#include<stdio.h>
int main()
{
  int i,n,sum=0,num;
  float avg;

  printf("\nEnter How many Number you want?\n");
  scanf("%d",&n);

  printf("\nEnter elements one by one\n");
  for(i=0;i<n;++i)
   {
     scanf("%d",&num);
     sum = sum +num;
   }

  avg = sum/(float)n;

  printf("\nSum of %d Numbers = %d",n, sum);
  printf("\nAverage of %d Numbers = %.2f",n, avg);

  return 0;
}

Output

Enter How many Number you want?
5
Enter elements one by one
10
2
38
23
38

Sum of 5 Numbers = 111
Average of 5 Numbers = 22.20000

Program Explanation

  • In this program, we first declared four variables of int data type named i, n, sum, and num.
  • After this a variable float type was created named avg.
  • We took a number input from the user.
  • According to the number input by the user, we have to enter the number from the user. Like in this program, the user wanted to add 5 numbers and get the average, so in this program, input 5 numbers (10, 2, 38, 23, 38) from the user.
  • After taking the input of numbers from the user, we took the sum of these numbers.
  • After that we calculated the average.
  • First we printed sum of the numbers on the screen.
  • After that printed the average given numbers on the screen.
  • In this way our program to calculate addition and average is completed.

C Program to Find the Sum and Average of n Numbers Using Function

To Calculate the sum and average of n numbers through a function, first you need to know about the function. You can read about the function from the link given below.

Algorithm

  • Program Start
  • Declaring variables
  • Input Number
  • Calling Function to calculate Sum
  • Calling Function to calculate Average
  • Print the Sum of n Numbers
  • Print the Average of n Numbers
  • Program End

Program

//C Program to Find the Sum and Average of n Numbers Using Function

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

  printf("\nEnter How many Number you want?\n");
  scanf("%d",&n);
 
   s = sum(n);                     //calling function to calculate sum

  avg = average(s,n);
      //calling function to calculate Average

  printf("\nSum of %d Numbers = %d",n, s);
  printf("\nAverage of %d Numbers = %.2f",n, avg);

  return 0;
}

int sum(int n)
{
  int i, sum=0, num;

  printf("\nEnter elements one by one\n");
  for(i=0;i<n;i++)
   {
     scanf("%d",&num);
     sum = sum +num;
   }
   return (sum);
}

int average(int s, int n)
{
  float avg;
  avg = s/(float)n;
  return (avg);
}

Output

Enter How many Number you want?
5
Enter elements one by one
10
37
92
25
72

Sum of 5 Numbers = 236
Average of 5 Numbers = 47.20000

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

Algorithm

  • Program Start
  • Declaring Variables (i,n,sum, num)
  • Input a Numbers from User
  • Calculating Sum of n Numbers
  • Calculating Average of n Numbers
  • Displaying the Sum of n Numbers
  • Displaying the Average of n Numbers
  • Program End

Program

//C Program to Find the Sum and Average of n Numbers Using While Loop

#include<stdio.h>
int main()
{
  int i = 0, n, sum=0, num;
  float avg;

  printf("\nEnter How many Number you want?\n");
  scanf("%d",&n);

  printf("\nEnter elements one by one\n");
  while(i<n)
   {
     scanf("%d",&num);
     sum = sum +num;
     i++;
   }

  avg=sum/(float)n;

  printf("\nSum of %d Numbers = %d",n, sum);
  printf("\nAverage of %d Numbers = %.2f",n, avg);

  return 0;
}

Output

Enter How many Number you want?
5

Enter elements one by one
10
20
30
25
27

Sum of 5 Numbers = 112
Average of 5 Numbers = 22.40000

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

Algorithm

  • Program Start
  • Declaring Variables (i,n,sum, num)
  • Input a Numbers from User
  • Calculating Sum of n Numbers
  • Calculating Average of n Numbers
  • Displaying the Sum of n Numbers
  • Displaying the Average of n Numbers
  • Program End

Program

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

#include<stdio.h>
int main()
{
  int i = 0, n, sum=0, num;
  float avg;

  printf("\nEnter How many Number you want?\n");
  scanf("%d",&n);

  printf("\nEnter elements one by one\n");
  do
   {
     scanf("%d",&num);
     sum = sum +num;
     i++;
   }while(i<n);

  avg = sum/(float)n;

  printf("\nSum of %d Numbers = %d",n, sum);
  printf("\nAverage of %d Numbers = %.2f",n, avg);

  return 0;
}

Output

Enter How many Number you want?
5

Enter elements one by one
15
25
3
25
27

Sum of 5 Numbers = 95
Average of 5 Numbers = 19.000000

C Program to Find the Sum and Average of n Numbers Using Array

To find the sum and average of n numbers through an Array, first you need to know about the Array. You can read about the Array from the link given below.

  • Array in C Language

Algorithm

  • Program Start
  • Declaring variables
  • Input Numbers
  • Calculating Sum
  • Calculating Average
  • Print the Sum of n Numbers
  • Print the Average of n Numbers
  • Program End

Program

//C Program to Find the Sum and Average of n Numbers Using Array      

#include<stdio.h>
void main()
{
  //Declaring  Variables

   int n, i, sum = 0, a[n];
   float avg;

  printf("\nEnter How many Number you want?\n");
  scanf("%d",&n);

  printf("\nEnter elements one by one\n");
  for(i=0; i<n; i++)
   {
    scanf("%d",&a[i]);
    sum = sum + a[i];  //Input Numbers
   }
   avg=sum/(float)n;

   printf("Sum of Three Numebers is : %d", sum);

   printf("\n Average of Three Numebers is : %f", avg);

}

Output

Enter How many Number you want?
3

Enter elements one by one
10
2
38

Sum of 5 Numbers = 50
Average of 5 Numbers = 16.666666

Conclusion

So friends, in this article, we have made a c program to calculate the sum and average of n numbers in five different ways.

  • C Program to Find the Sum and Average of n Numbers (Simple Way)
  • C Program to Find the Sum and Average of n Numbers Using Function
  • C Program to Find the Sum and Average of n Numbers Using while Loop
  • C Program to Find the Sum and Average of n Numbers Using Do while Loop
  • C Program to Find the Sum and Average of n Numbers Using Array

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