Write a program that reads 3 integers number on the keyboard without telling the user to enter the values and then displays on the screen the value of the closest integer to their average and then go to a new line.


Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include <math.h>
int main()
{
    int A, B ,C ;
    double avg;
    scanf("%d %d %d",&A, &B, &C);
    
    avg = (A+B+C)/3.0;
   
    if(fabs(A-avg) < fabs(B-avg)  &&  fabs(A-avg) < fabs(C-avg) )
        printf("%d\n",A);
    else if(fabs(B-avg) < fabs(A-avg)  &&  fabs(B-avg) < fabs(C-avg) )
        printf("%d\n",B);
    else
        printf("%d\n",C);

    return 0;
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Sorting an array using two stacks