In this code snippet, you will learn how to find the average of two integer numbers in C. the program is fairly easy and should be a great resource for new C learners.
Average of Two Integer Numbers in C
The program logic is easy:
- The user is prompted to enter two integer numbers, x, and y
- The numbers are added together, then divided by 2. i.e (x+ y)/2
- The result of the average two numbers is printed to the console.
Implementation
#include <stdio.h> int main() { int x, y; float avg; printf("Enter the 1st number:"); scanf("%d", & x); printf("You entered %d for the first number", x); printf("\nEnter the 2nd number:"); scanf("%d", & y); printf("You entered %d for the 2nd number", y); avg = (float)(x + y) / 2; printf("\n-------------------"); printf("\nThe average of %d and %d is %f", x, y, avg); return 0; }
Note: please make sure to enter Integer numbers in order for the program to work properly.
Have a question? Please type it below in the comment box. Happy learning!

Blog Hero