Program to find Sum and Average of three Numbers
Sum and Average Of 3 Numbers :
C Program to find the sum and average of given three numbers:-
Here in this blog post, let us discuss how to write a basic C program to find the sum and average of 3 numbers.
This post includes the two different programs to find the sum and average for integer values and for the float values.
Program to find the Sum and average of 3 given integer values:-
The below is a Data input and output program which takes the given input from the console and performs the operation and then prints the output to the console.
Program: -
#include <stdio.h>
void main() {
int num1, num2, num3;
int sum, avg;
printf("Enter three integer values : ");
scanf("%d %d %d", &num1, &num2, &num3);
sum = num1 + nnum2 + num3;
printf("sum of %d, %d and %d : %d\n", num1, num2, num3, sum);
avg = sum / 3;
printf("Average of %d, %d and %d : %d\n", num1, num2, num3, avg);
}
Output: -
Enter three integer values : 40 20 30
sum of 40, 20 and 30 : 90
Average of 40, 20 and 30 : 30
Program to find the Sum and Average of two given integer values |
Program: -
#include <stdio.h>
void main() {
int num1 = 12;
int num2 = 16;
int num3 = 21;
int sum;
int avg;
sum = num1 + num2 + num3;
printf("sum of %d , %d and %d : %d\n", num1, num2, num3, sum);
avg = sum / 3;
printf("Average of %d, %d and %d : %d\n", num1, num2, num3, avg);
}
Output: -
sum of 12, 16 and 21 : 49
Average of 12, 16 and 21 : 16
Program to find the Sum and Average of two given integer values |
__________________________________________________________________________
The above two programs only gives the approximate average values.
To get the Accurate values follow the code given below:-
Program: -
#include <stdio.h>
void main() {
int num1, num2, num3, sum;
float avg;
printf("Enter the three integer values : ");
scanf("%d %d %d", &num1, &num2, &num3);
sum = num1 + num2 + num3;
printf("sum of %d, %d and %d : %d", num1, num2, num3, sum);
avg = sum / 3.0;
printf("Average of %d, %d and %d :%d", num1, num2, num3, avg);
}
Output: -
Enter three integer values : 12 21 16
sum of 12, 21 and 16 : 49
Average of 12, 21 and 16 : 16.333334
Comments
Post a Comment
Leave your Quires here!