Google News
logo
C Program to find the Average of Two Numbers
Here we will write two C programs to find the average of two numbers(entered by user).

Program to find the average of two numbers :

Program :
#include <stdio.h>
int main()
{
    int num1, num2;
    float avg;

    printf("Enter first number: ");
    scanf("%d",&num1);
    printf("Enter second number: ");
    scanf("%d",&num2);

    avg= (float)(num1+num2)/2;

    //%.2f is used for displaying output upto two decimal places
    printf("Average of %d and %d is: %.2f",num1,num2,avg);

    return 0;
}
Output :
Enter first number: 16
Enter second number: 39
Average of 16 and 39 is: 27.50

Program to find the average using function :

In this program, we have created a user defined function average() for the calculation of average. The numbers entered by user are passed to this function during function call.
Program :
#include <stdio.h>
float average(int a, int b){
    return (float)(a+b)/2;
}
int main()
{
    int num1, num2;
    float avg;

    printf("Enter first number: ");
    scanf("%d",&num1);
    printf("Enter second number: ");
    scanf("%d",&num2);

    avg = average(num1, num2);

    //%.2f is used for displaying output upto two decimal places
    printf("Average of %d and %d is: %.2f",num1,num2,avg);

    return 0;
}
Output :
Enter first number: 12
Enter second number: 19
Average of 12 and 19 is: 15.50