Write a C Program to Find Average of Two Numbers with an example. This C example allows to enter two Integers and calculates the sum and average of that numbers.
#include <stdio.h> int main() { int number1, number2; float res; printf("Enter the First Number to find = "); scanf("%d",&number1); printf("Enter the Second Number to find = "); scanf("%d",&number2); int sm = number1 + number2; res = sum/2; //(float)sum/2; printf("The Sum of %d and %d = %d\n", number1, number2, sm); printf("The Average of %d and %d = %.2f\n", number1, number2, res); return 0; }
C average of two numbers output
Enter the First Number to find Average = 20
Enter the Second Number to find Average = 60
The Sum of 20 and 60 = 80
The Average of 20 and 60 = 40.00
In this C program, we created a new function that calculates and returns the average of given two numbers.
#include <stdio.h> float calcAverageofTwo(int a, int b) { return (float)(a + b)/2; } int main() { int number1, number2; printf("Enter the First Number to find Average = "); scanf("%d",&number1); printf("Enter the Second Number to find Average = "); scanf("%d",&number2); float average = calcAverageofTwo(number1, number2); printf("The Average of %d and %d = %.2f\n", number1, number2, average); return 0; }