Introduction
The else-if ladder is used when a C program needs to check multiple conditions one after another. It is especially useful for grading systems, age categories, menus, discounts, salary ranges, and other decision-making programs. In this chapter, you will practice if, else-if, else, and multiple conditions using relational and logical operators. The examples start with simple conditions and gradually move toward practical programs. else-if Ladder and Multiple Conditions in C practice questions with solutions to help you understand the concepts.
Q1. Check a Number as Positive, Negative, or Zero
Problem Statement
Write a C program that accepts a number and determines whether it is positive, negative, or zero using an else-if ladder.
C Program
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0)
{
printf("The number is positive.");
}
else if (number < 0)
{
printf("The number is negative.");
}
else
{
printf("The number is zero.");
}
return 0;
}
Sample Output
Enter a number: -15
The number is negative.
Explanation
The program checks the conditions from top to bottom:
number > 0number < 0- Otherwise, the number must be
0.
Only the first true condition’s block is executed.
Concepts Covered
ifelse-ifelse- Multiple conditions
- Relational operators
Q2. Display Grade Based on Marks
Problem Statement
Write a C program that accepts marks and displays a grade using the following rules:
| Marks | Grade |
|---|---|
| 90–100 | A |
| 80–89 | B |
| 70–79 | C |
| 60–69 | D |
| Below 60 | F |
C Program
#include <stdio.h>
int main()
{
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 90)
{
printf("Grade A");
}
else if (marks >= 80)
{
printf("Grade B");
}
else if (marks >= 70)
{
printf("Grade C");
}
else if (marks >= 60)
{
printf("Grade D");
}
else
{
printf("Grade F");
}
return 0;
}
Sample Output
Enter your marks: 85
Grade B
Explanation
The conditions are checked from highest to lowest.
For example, if the marks are 85:
85 >= 90 → False
85 >= 80 → True
So Grade B is displayed, and the remaining conditions are not checked.
Concepts Covered
else-ifladder- Range-based decisions
- Relational operators
- Grading system
Q3. Categorize Age
Problem Statement
Write a C program to categorize a person’s age:
- Below 13 → Child
- 13–19 → Teenager
- 20–59 → Adult
- 60 or above → Senior
C Program
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age < 13)
{
printf("Category: Child");
}
else if (age < 20)
{
printf("Category: Teenager");
}
else if (age < 60)
{
printf("Category: Adult");
}
else
{
printf("Category: Senior");
}
return 0;
}
Sample Output
Enter your age: 17
Category: Teenager
Explanation
Notice that we don’t need to write age >= 13 && age < 20 for the second condition.
The first condition has already established that the age is 13 or above when the program reaches the second condition.
Concepts Covered
else-if- Age ranges
- Multiple conditions
- Logical reasoning
Q4. Find the Largest of Three Numbers
Problem Statement
Write a C program that accepts three numbers and displays the largest number.
C Program
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
printf("Enter third number: ");
scanf("%d", &c);
if (a >= b && a >= c)
{
printf("%d is the largest.", a);
}
else if (b >= a && b >= c)
{
printf("%d is the largest.", b);
}
else
{
printf("%d is the largest.", c);
}
return 0;
}
Sample Output
Enter first number: 45
Enter second number: 72
Enter third number: 60
72 is the largest.
Explanation
The program uses the logical AND operator:
a >= b && a >= c
This means both conditions must be true for a to be the largest.
The same logic is then applied to b.
Concepts Covered
else-if- Logical AND
&& - Relational operators
- Comparing three values
Q5. Check a Number Range Using Multiple Conditions
Problem Statement
Write a C program that checks a number and displays:
Smallif it is between 1 and 10Mediumif it is between 11 and 50Largeif it is greater than 50Invalidif it is 0 or negative
C Program
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number >= 1 && number <= 10)
{
printf("Small");
}
else if (number >= 11 && number <= 50)
{
printf("Medium");
}
else if (number > 50)
{
printf("Large");
}
else
{
printf("Invalid number");
}
return 0;
}
Sample Output
Enter a number: 35
Medium
Explanation
The && operator allows two conditions to be checked together.
number >= 11 && number <= 50
Both conditions must be true.
Concepts Covered
- Multiple conditions
- Logical AND
&& else-if- Number ranges
Q6. Calculate Discount Based on Purchase Amount
Problem Statement
Create a program that calculates a discount according to the purchase amount:
| Purchase Amount | Discount |
|---|---|
| ₹10,000 or more | 20% |
| ₹5,000–₹9,999 | 10% |
| ₹2,000–₹4,999 | 5% |
| Below ₹2,000 | No discount |
C Program
#include <stdio.h>
int main()
{
float amount;
float discount;
float finalAmount;
printf("Enter purchase amount: ");
scanf("%f", &amount);
if (amount >= 10000)
{
discount = amount * 0.20;
}
else if (amount >= 5000)
{
discount = amount * 0.10;
}
else if (amount >= 2000)
{
discount = amount * 0.05;
}
else
{
discount = 0;
}
finalAmount = amount - discount;
printf("Discount = %.2f\n", discount);
printf("Final Amount = %.2f", finalAmount);
return 0;
}
Sample Output
Enter purchase amount: 7500
Discount = 750.00
Final Amount = 6750.00
Explanation
The program checks the highest discount first.
For ₹7500:
7500 >= 10000 → False
7500 >= 5000 → True
Therefore, a 10% discount is applied.
Concepts Covered
else-ifladder- Multiple conditions
- Percentage calculation
float- Arithmetic operators
Q7. Check Whether a Year Is a Leap Year
Problem Statement
Write a C program to determine whether a year is a leap year.
A year is a leap year when:
- It is divisible by
400, or - It is divisible by
4but not divisible by100.
C Program
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0)
{
printf("%d is a leap year.", year);
}
else if (year % 100 == 0)
{
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0)
{
printf("%d is a leap year.", year);
}
else
{
printf("%d is not a leap year.", year);
}
return 0;
}
Sample Output
Enter a year: 2024
2024 is a leap year.
Explanation
The program uses the modulus operator % to check divisibility.
For example:
2024 % 4 == 0
is true, so 2024 is a leap year.
Concepts Covered
else-if- Multiple conditions
- Modulus operator
- Divisibility
- Logical decision-making
Q8. Check Login Status Using Multiple Conditions
Problem Statement
Create a simple program that accepts a username status and password status.
Use:
1for correct username1for correct password0for incorrect
Display the appropriate message.
C Program
#include <stdio.h>
int main()
{
int usernameCorrect;
int passwordCorrect;
printf("Is username correct? Enter 1 for Yes, 0 for No: ");
scanf("%d", &usernameCorrect);
printf("Is password correct? Enter 1 for Yes, 0 for No: ");
scanf("%d", &passwordCorrect);
if (usernameCorrect == 1 && passwordCorrect == 1)
{
printf("Login successful.");
}
else if (usernameCorrect == 1 && passwordCorrect == 0)
{
printf("Incorrect password.");
}
else if (usernameCorrect == 0 && passwordCorrect == 1)
{
printf("Incorrect username.");
}
else
{
printf("Incorrect username and password.");
}
return 0;
}
Sample Output
Is username correct? Enter 1 for Yes, 0 for No: 1
Is password correct? Enter 1 for Yes, 0 for No: 0
Incorrect password.
Explanation
The program combines two conditions using &&.
For example:
usernameCorrect == 1 && passwordCorrect == 1
is true only when both values are 1.
Concepts Covered
- Multiple conditions
- Logical AND
else-if- User input
- Decision making
Q9. Determine Temperature Category
Problem Statement
Write a C program that categorizes temperature as follows:
- Below 0°C → Freezing
- 0°C–14°C → Cold
- 15°C–29°C → Moderate
- 30°C–39°C → Hot
- 40°C or above → Very Hot
C Program
#include <stdio.h>
int main()
{
int temperature;
printf("Enter temperature: ");
scanf("%d", &temperature);
if (temperature < 0)
{
printf("Freezing");
}
else if (temperature < 15)
{
printf("Cold");
}
else if (temperature < 30)
{
printf("Moderate");
}
else if (temperature < 40)
{
printf("Hot");
}
else
{
printf("Very Hot");
}
return 0;
}
Sample Output
Enter temperature: 32
Hot
Explanation
The conditions are checked from top to bottom.
For 32:
32 < 0 → False
32 < 15 → False
32 < 30 → False
32 < 40 → True
Therefore, the output is Hot.
Concepts Covered
else-ifladder- Multiple ranges
- Relational operators
- Conditional classification
Q10. Create a Student Result and Performance Program
Problem Statement
Write a C program that accepts marks in three subjects.
First check whether the student passed all subjects. A student must score at least 40 in every subject.
If the student passes, calculate the average and display:
Excellent→ average 80 or aboveVery Good→ average 70–79Good→ average 60–69Pass→ average below 60
If the student fails any subject, display Fail.
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 && science >= 40 && computer >= 40)
{
average = (math + science + computer) / 3.0;
printf("Average = %.2f\n", average);
if (average >= 80)
{
printf("Performance: Excellent");
}
else if (average >= 70)
{
printf("Performance: Very Good");
}
else if (average >= 60)
{
printf("Performance: Good");
}
else
{
printf("Performance: Pass");
}
}
else
{
printf("Result: Fail");
}
return 0;
}
Sample Output
Enter Mathematics marks: 85
Enter Science marks: 78
Enter Computer marks: 90
Average = 84.33
Performance: Excellent
Explanation
The first condition uses three conditions together:
math >= 40 && science >= 40 && computer >= 40
All three conditions must be true.
After the student passes all subjects, another else-if ladder determines the performance level from the average.
Concepts Covered
ifelseelse-if- Multiple conditions
- Logical AND
- Arithmetic operators
- Average calculation
- Nested decision-making
Key Takeaways
- An
else-ifladder is useful for checking multiple alternative conditions. - Conditions are evaluated from top to bottom.
- Once a condition is true, its block executes and the remaining
else-ifconditions are skipped. elseprovides the default case when none of the previous conditions is true.&&means logical AND; all combined conditions must be true.||means logical OR; at least one condition must be true.- Relational operators such as
>,<,>=,<=,==, and!=are commonly used with conditional statements. - The order of conditions matters, especially when checking ranges.
- An
else-ifladder can be placed inside anotherifblock when more detailed decision-making is required. - Avoid unnecessarily complicated conditions when a simpler
else-ifstructure can express the same logic.
FAQs
1. What is an else-if ladder in C?
An else-if ladder is a sequence of conditions where C checks each condition from top to bottom until it finds a true condition.
2. How many else-if statements can we use?
C does not impose a small fixed limit on the number of else-if clauses you can write. However, very long chains can make a program difficult to read and maintain.
3. What happens when two else-if conditions are true?
Only the first true condition in the if/else-if chain is executed.
For example:
if (marks >= 60)
{
printf("Condition 1");
}
else if (marks >= 50)
{
printf("Condition 2");
}
If marks is 70, only Condition 1 is printed.
4. What is the difference between multiple if statements and an else-if ladder?
With multiple independent if statements, more than one condition can execute.
With an else-if ladder, only the first true condition executes.
5. Why is condition order important?
Because C checks conditions from top to bottom.
For example:
if (marks >= 40)
{
printf("Pass");
}
else if (marks >= 80)
{
printf("Excellent");
}
The second condition will never be reached for marks of 80 or above because marks >= 40 is already true.
6. How do I check multiple conditions in one if statement?
Use logical operators.
For example, && requires all conditions to be true:
if (age >= 18 && marks >= 50)
{
printf("Eligible");
}
|| requires at least one condition to be true:
if (marks >= 90 || attendance >= 90)
{
printf("Eligible");
}
7. Can an else-if ladder contain an else?
Yes. The final else is optional. When included, it executes when none of the preceding if or else-if conditions is true.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
