Monday, April 28, 2025

C Tutorial - 8 - If..Else with User Input

#include <stdio.h>

int main()
{
    int first;
    int second;

    printf("Enter First Number: \n");
    scanf("%d", &first);

    printf("Enter Second Number: \n");
    scanf("%d", &second);

    if (first < second)
    {
        printf("%d is smaller than %d \n", first, second);
    }
    else if (first > second)
    {
        printf("%d is bigger than %d \n", first, second);
    }
    else
        printf("%d and %d are equal \n", first, second);

    return 0;
}

Result:

Enter First Number:
10
Enter Second Number:
20
10 is smaller than 20

Process returned 0 (0x0)   execution time : 4.561 s
Press any key to continue.

This C code prompts the user to enter two integer values for the variables first and second, and then uses an if-else statement to compare the values of these variables.

First, the program prompts the user to enter the value of first and second using scanf() function. Then, it checks if first is smaller than second using the if statement. If this condition is true, the program prints a message stating that first is smaller than second using printf() function. If the if condition is false, the program moves to the else if statement, which checks if first is greater than second. If this condition is true, the program prints a message stating that first is bigger than second using printf() function. If both the if and else if conditions are false, the program executes the else statement and prints a message stating that both the numbers are equal using printf() function.

Finally, the program returns 0 to indicate that the program has executed successfully.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...