Q:

C Program to Find the Sum and Average of n Numbers

belongs to collection: Basic C Programming Examples

0

you have to make this program in the following 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

All Answers

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

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

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

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

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

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

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 Smallest of Three Numbers... >>
<< C Program to Find the Sum and Average of Three Num...