Conditional Statement (If-Else)

Definition:

The if-else statement is used to execute one block of code when a condition is true and another block of code when the condition is false. It helps in making decisions based on the given condition.


Program: Check Whether a Number is Even or Odd

Explanation: This program accepts a number from the user and determines whether it is even or odd using the if-else statement.


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

main()
{
    int no;

    clrscr();

    printf("Enter the Number : ");
    scanf("%d",&no);

    if(no % 2 == 0)
    {
        printf("\nThe Number is Even");
    }
    else
    {
        printf("\nThe Number is Odd");
    }

    getch();
}

Output:

Enter the Number : 24

The Number is Even

Practice Exercises

Task 1: Write a C program to accept a number from the user and check whether it is divisible by 5 or not using the if-else statement.

Goal: Learn how to use the modulus (%) operator with the if-else statement.
💡 Hint: If the remainder after dividing the number by 5 is 0, then the number is divisible by 5.
💡 Show Solution

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

main()
{
    int no;

    clrscr();

    printf("Enter the Number : ");
    scanf("%d",&no);

    if(no % 5 == 0)
    {
        printf("\nNumber is Divisible by 5");
    }
    else
    {
        printf("\nNumber is Not Divisible by 5");
    }

    getch();
}

Output
Enter the Number : 25

Number is Divisible by 5
Explanation: The program accepts a number from the user and uses the modulus (%) operator to check whether it is divisible by 5. If the remainder is 0, it displays "Number is Divisible by 5"; otherwise, it displays "Number is Not Divisible by 5".