Q:

C Program to Find the Sum and Average of Three 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 Three Numbers (Simple Way)
  • C Program to Find the Sum and Average of Three Numbers Using Function
  • C Program to Find the Sum and Average of Three 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 Three Numbers

To get the sum and average of three numbers, we have to first take three numbers from the user, then add these numbers and find their average.

formula to calculate sum and average of three numbers

Sum = a+b+c;

Average = sum/count

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

Algorithm

  • Program Start
  • Declaring Variables
  • Input Three Numbers from User
  • Calculating Sum of Three Numbers (sum = x + y + z)
  • Displaying the Sum of Three Numbers
  • Calculating Average of Three Numbers (average = sum/count)
  • Displaying the Average of Three Numbers
  • Program End

Program

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

#include<stdio.h>
int main()
{
  //Declaring Three Variables
  
    int x, y, z, sum;           
   float avg;

   printf("Enter Three Numbers : \n");    
   scanf("%d %d %d",&x, &y, &z);     //Input Numbers

  //Calculating Sum of three numbers

   sum = x + y +z;
   printf("Sum of Three Numebers is : %d", sum);    

  //Calculating Average of three numbers

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

   return 0;
}

Output

Enter Three Numbers :
10
2
38

Sum of Three Numebers is : 50
Average of Three Numebers is : 16.000000

Program Explanation

  • In this program, we first declared four variables x, y, z, sum.
  • After that created another variable named avg with the help of float data type.
  • In this avg variable, we will store the average of the number three and in the sum variable, we will store the sum of the number three.
  • After declaring the variables, we got input three numbers from the user and store those values ​​in the x, y, z variables.
  • After this, we Calculate the sum of three numbers using the formula (sum = x + y + z ) and stored its result in the sum variable.
  • After that, we print the sum of three numbers.
  • After this, we Calculate the average of three numbers using the formula (average = sum/count) and stored its result in the avg variable.
  • Finally, we printed the stored value in this avg variable on the screen with the help of printf() function.

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

Algorithm

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

Program

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

#include<stdio.h>
int sum(int, int, int);
void average(int);
int main()
{
 //Declaring Variables
   int x, y, z, s;

  //Input Numbers
   printf("Enter Three Numbers : \n");
   scanf("%d %d %d",&x, &y, &z);

 //Calling Function to find the Sum of three numbers
   s = sum(x,y,z);
   printf("Sum of Three Numbers is : %d \n", s);

 //Calling Function to find the Average of three numbers
   average(s);

}
int sum(int a, int b, int c)
{
    return (a+b+c);
}
void  average(int sum)
{   float avg;
    avg=sum/3;
    printf("Average of Three Numebers is : %f", avg);
}

Output

Enter Three Numbers : 
10
38
23

Sum of Three Numbers is : 71
Average of Three Numebers is : 23.00000

 

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

Algorithm

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

Program

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

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

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

   printf("Enter Three Numbers : \n");
   for(i=0; i<3; i++)
   {
    scanf("%d",&a[i]);  //Input Numbers
   }

   for(i=0; i<3;i++)
   {
    sum = sum + a[i];  //Input Numbers
   }
   printf("Sum of Three Numebers is : %d", sum);

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

}

Output

Enter Three Numbers :
38
23
38

Sum of Three Numbers is : 99
Average of Three Numebers is : 33.000000

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 the Sum and Average of n Numbers... >>
<< C Program to Find Average of n Odd and Even Number...