How to compare the maximum value of three numbers in the C language?

You can use an if statement to compare the maximum of three numbers. Here is an example code:

#include <stdio.h>

int main() {
    int num1, num2, num3;
    printf("请输入三个整数:\n");
    scanf("%d %d %d", &num1, &num2, &num3);

    int max = num1;

    if (num2 > max) {
        max = num2;
    }
    if (num3 > max) {
        max = num3;
    }

    printf("最大值是:%d\n", max);

    return 0;
}

The code first requires users to input three integers, then uses if statements to compare each of the three numbers with the current maximum value. If there is a larger number, it updates the maximum value. Finally, it outputs the maximum value.

bannerAds