Complementary task for topic: 2

M Nemeth · 2023-08-29 15:21:04.605220'

If/else I.

If/else I.

Task: Determine the largest of three numbers:
Write a C program that prompts the user to enter three numbers and determines the largest among them using if/else statements. Display the result using printf().

Hint: simple conditions, but take care about the else statements!

Solution
#include 

int main() {
    int num1, num2, num3;
    
    printf("Enter the first number: ");
    scanf("%d", &num1);
    
    printf("Enter the second number: ");
    scanf("%d", &num2);
    
    printf("Enter the third number: ");
    scanf("%d", &num3);
    
    if (num1 >= num2 && num1 >= num3) {
        printf("The largest number is %d.\n", num1);
    } else if (num2 >= num1 && num2 >= num3) {
        printf("The largest number is %d.\n", num2);
    } else {
        printf("The largest number is %d.\n", num3);
    }
    
    return 0;
}



Explanation
In this example, the program uses printf() to prompt the user to enter three numbers. The scanf() function is used to scan the input from the user and store it in the variables num1, num2, and num3. The program uses if/else statements to compare the three numbers and determine the largest among them. The corresponding message is printed using printf() based on the condition that evaluates to true.
< < previous    next > >