Skip to content
C Programming – Multiple If and If Else

C Programming

Multiple If and If…Else Statements

Learning Objectives

After studying this topic, you will be able to:

  • Understand the multiple if statement.
  • Understand the if…else statement.
  • Use relational and logical operators in conditions.
  • Write simple C programs using decision-making statements.
  • Develop algorithms and flowcharts for simple problems.

1. Multiple If Statement

Definition:

A multiple-if structure consists of more than one simple if statement. It is used when we want to check different independent conditions and execute the corresponding block when a condition is true.

Syntax

if (condition1)
{
    // statements
}

if (condition2)
{
    // statements
}

if (condition3)
{
    // statements
}

Important Point

In multiple if statements, every condition is checked independently. Therefore, more than one if block can execute if multiple conditions are true.

2. Example: Positive, Negative or Zero

The following program checks whether the entered number is positive, negative, or equal to zero.

#include <stdio.h>

int main()
{
    int a;

    printf("Enter any number: ");
    scanf("%d", &a);

    if (a == 0)
    {
        printf("Your number is Zero");
    }

    if (a > 0)
    {
        printf("%d is positive", a);
    }

    if (a < 0)
    {
        printf("%d is negative", a);
    }

    printf("\nThe End");

    return 0;
}

Example Output

Enter any number: 25
25 is positive
The End
Note:

Modern C programs generally use int main() and return 0; instead of void main().

3. Exercise: Display Number in Words

Write an algorithm, draw a flowchart, and write a C program to accept any one number from 2, 4, 6, or 8 and display its corresponding word.

Algorithm

  1. Start.
  2. Declare an integer variable n.
  3. Accept a number from the user.
  4. If n == 2, display TWO.
  5. If n == 4, display FOUR.
  6. If n == 6, display SIX.
  7. If n == 8, display EIGHT.
  8. If the number is not 2, 4, 6, or 8, display Invalid Input.
  9. Stop.

Flowchart

START
Input n
Is n = 2?
↓ Yes
Display TWO
Is n = 4?
↓ Yes
Display FOUR
Is n = 6?
↓ Yes
Display SIX
Is n = 8?
↓ Yes
Display EIGHT
STOP

C Program

#include <stdio.h>

int main()
{
    int n;

    printf("Enter any number 2, 4, 6 or 8 only: ");
    scanf("%d", &n);

    if (n == 2)
    {
        printf("TWO");
    }

    if (n == 4)
    {
        printf("FOUR");
    }

    if (n == 6)
    {
        printf("SIX");
    }

    if (n == 8)
    {
        printf("EIGHT");
    }

    if (n != 2 && n != 4 && n != 6 && n != 8)
    {
        printf("Invalid Input");
    }

    printf("\nThank You");

    return 0;
}

Example

Enter any number 2, 4, 6 or 8 only: 6
SIX
Thank You
Logical AND (&&):

The expression n != 2 && n != 4 && n != 6 && n != 8 is true only when the entered number is different from all four valid choices.

4. if…else Statement

Definition:

The if...else statement is used when we want to execute one of two different blocks. One block executes when the condition is true, and the other block executes when the same condition is false.

Syntax

if (condition)
{
    // statements executed when condition is true
}
else
{
    // statements executed when condition is false
}
Remember:
  • else is an optional part of if.
  • There can be an if without an else.
  • There cannot be an else without an if.
  • For one condition, only one of the two blocks executes.

5. Example: Find Greater Number

This program accepts two numbers and displays the greater number using if...else.

#include <stdio.h>

int main()
{
    int a, b;

    printf("Enter any two numbers: ");
    scanf("%d%d", &a, &b);

    if (a > b)
    {
        printf("%d is greater than %d", a, b);
    }
    else
    {
        printf("%d is greater than %d", b, a);
    }

    printf("\nThe End");

    return 0;
}

Example Output

Enter any two numbers: 15 9
15 is greater than 9
The End
Important:

The above program assumes that the two numbers are different. If equal numbers should also be handled separately, use an additional condition.

6. Multiple If vs if…else

Multiple If if…else
Checks multiple independent conditions. Chooses between two blocks based on one condition.
More than one block may execute. Only one block executes.
Uses separate if statements. Uses if followed by else.
Useful for independent checks. Useful when there are exactly two alternatives.

7. Operators Used in These Programs

Operator Meaning Example
== Equal to a == 10
!= Not equal to a != 10
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= 0
&& Logical AND a > 0 && b > 0
% Remainder / Modulus n % 2

8. Practice Questions

1. Rectangle or Square

Write a C program to determine whether a shape is a rectangle or a square based on user-entered dimensions.

Hint: len == bre

#include <stdio.h>

int main()
{
    float len, bre;

    printf("Enter length and breadth: ");
    scanf("%f%f", &len, &bre);

    if (len == bre)
    {
        printf("The shape is a Square");
    }
    else
    {
        printf("The shape is a Rectangle");
    }

    return 0;
}
2. Even or Odd

Write a C program to determine whether the user-entered number is even or odd.

Hint: n % 2 == 0

#include <stdio.h>

int main()
{
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);

    if (n % 2 == 0)
    {
        printf("%d is Even", n);
    }
    else
    {
        printf("%d is Odd", n);
    }

    return 0;
}
3. Big Number Out of Two

Write a C program to determine the bigger number out of two user-entered numbers.

#include <stdio.h>

int main()
{
    int a, b;

    printf("Enter two numbers: ");
    scanf("%d%d", &a, &b);

    if (a > b)
    {
        printf("%d is the bigger number", a);
    }
    else if (b > a)
    {
        printf("%d is the bigger number", b);
    }
    else
    {
        printf("Both numbers are equal");
    }

    return 0;
}
4. Greater Than or Equal to Zero

Write a C program to determine whether the given number is greater than or equal to zero or less than zero.

Hint: x >= 0

#include <stdio.h>

int main()
{
    int x;

    printf("Enter a number: ");
    scanf("%d", &x);

    if (x >= 0)
    {
        printf("The number is greater than or equal to zero");
    }
    else
    {
        printf("The number is less than zero");
    }

    return 0;
}
5. Discount Applicable

Write a C program to display “Discount Applicable” if the purchase amount is more than Rs. 5000. Otherwise, display “No Discount”.

#include <stdio.h>

int main()
{
    float amount;

    printf("Enter purchase amount: ");
    scanf("%f", &amount);

    if (amount > 5000)
    {
        printf("Discount Applicable");
    }
    else
    {
        printf("No Discount");
    }

    return 0;
}
6. Calculate Final Price

Write a C program to display the final price. If the price is more than Rs. 5000, give a discount of Rs. 50. Otherwise, display the actual price as the final price.

#include <stdio.h>

int main()
{
    float price, finalPrice;

    printf("Enter actual price: ");
    scanf("%f", &price);

    if (price > 5000)
    {
        finalPrice = price - 50;
    }
    else
    {
        finalPrice = price;
    }

    printf("Final Price = Rs. %.2f", finalPrice);

    return 0;
}
Example:

If the actual price is Rs. 6000, the final price will be Rs. 5950.

7. Uppercase or Lowercase Alphabet

Write a C program to check whether the user-entered alphabet is an uppercase or lowercase alphabet.

#include <stdio.h>

int main()
{
    char ch;

    printf("Enter an alphabet: ");
    scanf(" %c", &ch);

    if (ch >= 'A' && ch <= 'Z')
    {
        printf("%c is an Uppercase alphabet", ch);
    }
    else if (ch >= 'a' && ch <= 'z')
    {
        printf("%c is a Lowercase alphabet", ch);
    }
    else
    {
        printf("Invalid input. Please enter an alphabet.");
    }

    return 0;
}
Examples:
  • Input: A → Uppercase alphabet
  • Input: m → Lowercase alphabet
  • Input: 7 → Invalid input

9. Additional Practice Questions

  1. Write a C program to check whether a person’s age is eligible to vote.
  2. Write a C program to check whether a number is divisible by 5.
  3. Write a C program to check whether a student has passed or failed. Assume the passing mark is 35.
  4. Write a C program to check whether a given character is a vowel or consonant.
  5. Write a C program to check whether a number is positive or negative.
  6. Write a C program to check whether a person is eligible for a senior citizen discount if their age is 60 or above.
  7. Write a C program to check whether the entered number is divisible by both 3 and 5.
  8. Write a C program to compare two numbers and display whether the first number is greater than, less than, or equal to the second number.

10. Quick Revision

  • Multiple if: Used to check several independent conditions.
  • if…else: Used when there are two possible outcomes.
  • == checks equality.
  • != checks inequality.
  • > checks greater than.
  • < checks less than.
  • >= checks greater than or equal to.
  • && means logical AND.
  • % gives the remainder and is commonly used to check even/odd numbers.
Remember:

Use multiple if statements when conditions are independent. Use if...else when exactly one of two alternatives should be selected.

C Programming Notes — Decision Making Statements