C program to find the average of two numbers

In this program we will calculate the average of two integer numbers.

#include<stdio>
int main()
{
    int num1 = 12, num2 = 16, result;
    result = (num1 + num2)/2;
    printf("The average is %d", result);
    return 0;
}

Explanation

In this example we are using the mathematical formula to find average of numbers. Since we need to find the average of two numbers, we have divided the sum of these numbers by 2.

By far we have seen a number of programs, and from this point i consider that you guys have enough knowledge about  header files, printf & scanf functions and return type. So i skip the explanation about including the header file header file ‘stdio’.

Inside the main function, we have declared three variables x, y and result of integer type and initialized the variables num1 and num2 with values 12 and 16 respectively.

Next We have calculated the average by adding num1 and num2 and dividing the sum by 2 and assigned the calculated average to the variable result.

By taking input from the user

#include<stdio>
int main()
{
    int num1, num2, result;
    printf("Enter first number");
    scanf("%d", &num1);
    printf("Enter second number");
    scanf("%d", &num2);
    result = (num1 + num2)/2;
    printf("The average is %d", result);
    return 0;
}

This example works the same as the above except that in the below example, we have taken the input from the users and stored the inputs in the variables num1 and num2 by scanf function and then calculated the average.