Conditional Statement (If-Else)

Definition:

The if-else statement is a decision-making statement in C programming. It is used to execute one block of code when a condition is true and another block of code when the condition is false.

Program: Find the Greater of Two Numbers

Explanation: This program accepts two numbers from the user and displays the greater number using the if-else statement.


#include <stdio.h>
#include <conio.h>

main()
{
    int No1, No2;

    clrscr();

    printf("Enter the First No : ");
    scanf("%d",&No1);

    printf("Enter the Second No : ");
    scanf("%d",&No2);

    if(No1 > No2)
    {
        printf("\nNo 1 is greater");
    }
    else
    {
        printf("\nNo 2 is greater");
    }

    getch();
}

Output:

Enter the First No : 45
Enter the Second No : 25

No 1 is greater

Practice Exercises

Task 1: Write a C program to accept two numbers from the user and check whether the first number is greater than the second number using the if-else statement.

Goal: Learn how to compare two numbers using the if-else statement.
💡 Hint: Compare both numbers using the > operator.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

main()
{
    int a,b;

    clrscr();

    printf("Enter First Number : ");
    scanf("%d",&a);

    printf("Enter Second Number : ");
    scanf("%d",&b);

    if(a > b)
    {
        printf("\nFirst Number is Greater");
    }
    else
    {
        printf("\nFirst Number is Not Greater");
    }

    getch();
}

Output
Enter First Number : 30
Enter Second Number : 20

First Number is Greater
Explanation: The program accepts two numbers from the user and compares them using the > operator. If the first number is greater than the second number, it displays "First Number is Greater"; otherwise, it displays "First Number is Not Greater".