if, if-else and Nested if in C Practice Questions with Solutions

Introduction

Conditional statements allow a C program to make decisions based on conditions. The if statement runs code when a condition is true, if-else provides an alternative when the condition is false, and nested if statements allow one condition to be checked inside another. In this chapter, you will practice these statements using beginner-friendly examples such as checking numbers, age, marks, eligibility, and simple real-world conditions. if, if-else and Nested if in C practice questions with solutions to help you understand the concepts.

Q1. Check Whether a Number Is Positive

Problem Statement

Write a C program that takes a number from the user and checks whether the number is positive using an if statement.

C Program

#include <stdio.h>

int main()
{
    int number;

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

    if (number > 0)
    {
        printf("The number is positive.");
    }

    return 0;
}

Sample Output

Enter a number: 25
The number is positive.

Explanation

The condition:

number > 0

is checked by the if statement.

If the condition is true, the message is displayed.

Concepts Covered

  • if statement
  • Comparison operator
  • User input
  • Positive number checking

Q2. Check Whether a Number Is Positive or Negative

Problem Statement

Write a C program that checks whether a number is positive or negative using if-else.

C Program

#include <stdio.h>

int main()
{
    int number;

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

    if (number >= 0)
    {
        printf("The number is positive or zero.");
    }
    else
    {
        printf("The number is negative.");
    }

    return 0;
}

Sample Output

Enter a number: -12
The number is negative.

Explanation

If number >= 0 is true, the first block executes.

Otherwise, the else block executes.

Concepts Covered

  • if-else
  • Relational operator
  • Negative numbers
  • Conditional execution

Q3. Check Whether a Number Is Even or Odd

Problem Statement

Write a C program to check whether a number entered by the user is even or odd.

C Program

#include <stdio.h>

int main()
{
    int number;

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

    if (number % 2 == 0)
    {
        printf("The number is even.");
    }
    else
    {
        printf("The number is odd.");
    }

    return 0;
}

Sample Output

Enter a number: 17
The number is odd.

Explanation

The % operator returns the remainder.

For an even number, the remainder after dividing by 2 is 0.

number % 2 == 0

Concepts Covered

  • if-else
  • Modulus operator %
  • Even and odd numbers

Q4. Check Voting Age

Problem Statement

Write a C program that accepts a person’s age and checks whether the person has reached the age of 18.

C Program

#include <stdio.h>

int main()
{
    int age;

    printf("Enter your age: ");
    scanf("%d", &age);

    if (age >= 18)
    {
        printf("You are 18 or older.");
    }
    else
    {
        printf("You are below 18.");
    }

    return 0;
}

Sample Output

Enter your age: 20
You are 18 or older.

Explanation

The program checks:

age >= 18

If the condition is true, the first message is displayed. Otherwise, the else block runs.

Concepts Covered

  • if-else
  • Relational operator
  • Age comparison
  • User input

Q5. Check Whether a Student Passed

Problem Statement

Write a C program that accepts marks and checks whether the student passed. Consider 40 or above as passing.

C Program

#include <stdio.h>

int main()
{
    int marks;

    printf("Enter your marks: ");
    scanf("%d", &marks);

    if (marks >= 40)
    {
        printf("You passed.");
    }
    else
    {
        printf("You failed.");
    }

    return 0;
}

Sample Output

Enter your marks: 65
You passed.

Explanation

The condition is:

marks >= 40

A mark of 40 also satisfies the condition.

Concepts Covered

  • if-else
  • Comparison
  • Passing condition
  • User input

Q6. Find the Greater of Two Numbers

Problem Statement

Write a C program that accepts two numbers and displays the greater number using if-else.

C Program

#include <stdio.h>

int main()
{
    int a, b;

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

    printf("Enter second number: ");
    scanf("%d", &b);

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

    return 0;
}

Sample Output

Enter first number: 45
Enter second number: 72
72 is greater.

Explanation

The program checks three possibilities:

  1. a is greater than b.
  2. b is greater than a.
  3. Both numbers are equal.

Concepts Covered

  • if
  • else if
  • else
  • Multiple conditions
  • Comparing two numbers

Q7. Use Nested if to Check Eligibility

Problem Statement

A student can join a particular course if their age is at least 16. If the age requirement is satisfied, check whether the student has scored at least 50 marks.

Use a nested if statement.

C Program

#include <stdio.h>

int main()
{
    int age, marks;

    printf("Enter your age: ");
    scanf("%d", &age);

    printf("Enter your marks: ");
    scanf("%d", &marks);

    if (age >= 16)
    {
        if (marks >= 50)
        {
            printf("You are eligible.");
        }
        else
        {
            printf("Your marks are not sufficient.");
        }
    }
    else
    {
        printf("Your age is below the required age.");
    }

    return 0;
}

Sample Output

Enter your age: 17
Enter your marks: 72
You are eligible.

Explanation

The second if is placed inside the first if.

The program first checks:

age >= 16

Only when this condition is true does it check:

marks >= 50

This is called a nested if.

Concepts Covered

  • Nested if
  • if-else
  • Multiple conditions
  • Eligibility checking

Q8. Check Whether a Number Is Within a Range

Problem Statement

Write a C program to check whether a number is between 10 and 50, including both 10 and 50.

C Program

#include <stdio.h>

int main()
{
    int number;

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

    if (number >= 10)
    {
        if (number <= 50)
        {
            printf("The number is between 10 and 50.");
        }
        else
        {
            printf("The number is greater than 50.");
        }
    }
    else
    {
        printf("The number is less than 10.");
    }

    return 0;
}

Sample Output

Enter a number: 35
The number is between 10 and 50.

Explanation

This example uses nested if statements.

First:

number >= 10

is checked.

If that is true, the program checks:

number <= 50

Therefore, both conditions must be satisfied for the number to be inside the range.

Concepts Covered

  • Nested if
  • Range checking
  • Relational operators
  • Multiple conditions

Q9. Check Login Credentials Using Nested if

Problem Statement

Create a simple login program. First check whether the entered username is correct. If it is correct, check the password.

For this beginner example, use:

Username: admin
Password: 1234

C Program

#include <stdio.h>
#include <string.h>

int main()
{
    char username[20];
    int password;

    printf("Enter username: ");
    scanf("%19s", username);

    printf("Enter password: ");
    scanf("%d", &password);

    if (strcmp(username, "admin") == 0)
    {
        if (password == 1234)
        {
            printf("Login successful.");
        }
        else
        {
            printf("Incorrect password.");
        }
    }
    else
    {
        printf("Incorrect username.");
    }

    return 0;
}

Sample Output

Enter username: admin
Enter password: 1234
Login successful.

Explanation

The program first checks the username.

strcmp(username, "admin") == 0

If the username is correct, the nested if checks the password.

The program uses strcmp() because C does not compare strings with ==.

Concepts Covered

  • Nested if
  • String comparison
  • strcmp()
  • if-else
  • Header file <string.h>

Q10. Create a Simple Student Result Checker

Problem Statement

Create a program that accepts marks in three subjects.

The student passes only when:

  • Mathematics is at least 40
  • Science is at least 40
  • Computer is at least 40

If the student passes all three subjects, check the average marks and display:

  • Excellent if average is 80 or above
  • Good if average is 60 or above
  • Pass otherwise

Use nested if statements.

C Program

#include <stdio.h>

int main()
{
    int math, science, computer;
    float average;

    printf("Enter Mathematics marks: ");
    scanf("%d", &math);

    printf("Enter Science marks: ");
    scanf("%d", &science);

    printf("Enter Computer marks: ");
    scanf("%d", &computer);

    if (math >= 40)
    {
        if (science >= 40)
        {
            if (computer >= 40)
            {
                average = (math + science + computer) / 3.0;

                printf("Average = %.2f\n", average);

                if (average >= 80)
                {
                    printf("Result: Excellent");
                }
                else if (average >= 60)
                {
                    printf("Result: Good");
                }
                else
                {
                    printf("Result: Pass");
                }
            }
            else
            {
                printf("Failed in Computer.");
            }
        }
        else
        {
            printf("Failed in Science.");
        }
    }
    else
    {
        printf("Failed in Mathematics.");
    }

    return 0;
}

Sample Output

Enter Mathematics marks: 85
Enter Science marks: 78
Enter Computer marks: 90
Average = 84.33
Result: Excellent

Explanation

The program checks each subject one by one.

First:

if (math >= 40)

Then:

if (science >= 40)

Then:

if (computer >= 40)

Only after all three conditions are true does the program calculate the average.

The average is then checked using another if-else if-else structure.

Concepts Covered

  • Nested if
  • if-else
  • else-if
  • Multiple conditions
  • Arithmetic operators
  • User input
  • Average calculation
  • Formatted output

Key Takeaways

  • if is used to execute code when a condition is true.
  • if-else provides two possible execution paths.
  • A nested if means an if statement is placed inside another if or else block.
  • Conditions are commonly created using relational and logical operators.
  • C treats 0 as false and a non-zero value as true in conditional expressions.
  • == compares two values, while = assigns a value.
  • Braces {} are recommended even when an if block contains only one statement because they make the program easier to read and maintain.
  • Nested if statements are useful when one decision depends on another decision.
  • Complex nested conditions can become difficult to read, so logical operators or other structures may sometimes provide a clearer solution.
  • else belongs to the nearest unmatched if in C when braces do not make the association explicit.

FAQs

1. What is an if statement in C?

An if statement executes a block of code when its condition evaluates to true.

Example:

if (age >= 18)
{
    printf("Adult");
}

2. What is the difference between if and if-else?

An if statement provides an action when the condition is true.

An if-else statement provides two possible paths: one when the condition is true and another when it is false.

3. What is a nested if in C?

A nested if is an if statement inside another conditional block.

Example:

if (age >= 18)
{
    if (hasID == 1)
    {
        printf("Allowed");
    }
}

4. Can we use multiple if statements in C?

Yes. Multiple independent if statements can be used when each condition needs to be checked separately.

if (marks >= 40)
{
    printf("Pass");
}

if (marks >= 80)
{
    printf("Excellent");
}

5. What happens when an if condition is false?

If there is no else, the if block is skipped and program execution continues with the next statement.

If an else exists, the else block executes.

6. Can an if statement be placed inside another if?

Yes. This is called a nested if.

if (age >= 18)
{
    if (license == 1)
    {
        printf("Allowed to drive.");
    }
}

7. What is the difference between nested if and else-if?

A nested if places one decision inside another decision.

An else-if chain checks multiple alternative conditions.

For example:

if (marks >= 80)
{
    printf("A");
}
else if (marks >= 60)
{
    printf("B");
}
else
{
    printf("C");
}

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top