Conditional statements are one of the most important concepts in C++ programming. They allow a program to make decisions based on specific conditions. Instead of executing every statement sequentially, conditional statements enable programs to choose different execution paths depending on whether a condition is true or false.
In C++, decision-making is primarily achieved using:
ifstatementif...elsestatementelse ifladder- Nested
if switchstatement- Conditional (ternary) operator
These statements are widely used in real-world applications such as login systems, banking software, grading systems, e-commerce platforms, and competitive programming. C++ Conditional Statements practice questions with solutions help to build concepts.
In this chapter, you’ll solve practical problems that strengthen your understanding of decision-making using conditional statements. Every question includes:
- Problem Statement
- Complete C++ Solution
- Sample Input
- Sample Output
- Explanation
- Concepts Covered
Let’s begin with the fundamentals of conditional statements in C++.
1. C++ Program to Check Whether a Number is Positive, Negative, or Zero
Problem Statement
Write a C++ program to determine whether a number is positive, negative, or zero.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number: ";
cin >> number;
if (number > 0)
{
cout << "Positive Number";
}
else if (number < 0)
{
cout << "Negative Number";
}
else
{
cout << "Zero";
}
return 0;
}
Sample Input
Enter a number: -25
Sample Output
Negative Number
Explanation
The program checks three conditions:
- Greater than zero → Positive
- Less than zero → Negative
- Otherwise → Zero
Concepts Covered
- if…else if…else
- Comparison Operators
- Decision Making
2. C++ Program to Check Whether a Number is Even or Odd
Problem Statement
Write a C++ program to check whether a number is even or odd using an if-else statement.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number: ";
cin >> number;
if (number % 2 == 0)
{
cout << "Even Number";
}
else
{
cout << "Odd Number";
}
return 0;
}
Sample Input
Enter a number: 48
Sample Output
Even Number
Explanation
If the remainder after dividing by 2 is zero, the number is even; otherwise, it is odd.
Concepts Covered
- if…else
- Modulus Operator
- Arithmetic Operations
3. C++ Program to Find the Largest of Two Numbers
Problem Statement
Write a C++ program to find the larger of two numbers entered by the user.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
if (num1 > num2)
{
cout << "Largest Number = " << num1;
}
else
{
cout << "Largest Number = " << num2;
}
return 0;
}
Sample Input
Enter first number: 75
Enter second number: 62
Sample Output
Largest Number = 75
Explanation
The program compares both numbers using the greater-than (>) operator and prints the larger value.
Concepts Covered
- Comparison Operators
- if…else
- Decision Making
4. C++ Program to Find the Largest of Three Numbers
Problem Statement
Write a C++ program to determine the largest among three numbers.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
if (a >= b && a >= c)
{
cout << "Largest Number = " << a;
}
else if (b >= a && b >= c)
{
cout << "Largest Number = " << b;
}
else
{
cout << "Largest Number = " << c;
}
return 0;
}
Sample Input
Enter three numbers: 18 92 55
Sample Output
Largest Number = 92
Explanation
The program compares all three values using logical AND (&&) and prints the greatest value.
Concepts Covered
- else if Ladder
- Logical Operators
- Multiple Conditions
5. C++ Program to Check Whether a Year is a Leap Year
Problem Statement
Write a C++ program to check whether a given year is a leap year.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int year;
cout << "Enter a year: ";
cin >> year;
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
cout << year << " is a Leap Year";
}
else
{
cout << year << " is NOT a Leap Year";
}
return 0;
}
Sample Input
Enter a year: 2024
Sample Output
2024 is a Leap Year
Explanation
A year is considered a leap year if:
- It is divisible by 400, or
- It is divisible by 4 but not divisible by 100.
Concepts Covered
- Nested Conditions
- Logical Operators
- Modulus Operator
- Decision Making
6. C++ Program to Check Whether a Character is a Vowel or Consonant
Problem Statement
Write a C++ program to determine whether an alphabet entered by the user is a vowel or a consonant.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
char ch;
cout << "Enter an alphabet: ";
cin >> ch;
if (ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' ||
ch == 'O' || ch == 'U')
{
cout << "Vowel";
}
else
{
cout << "Consonant";
}
return 0;
}
Sample Input
Enter an alphabet: E
Sample Output
Vowel
Explanation
The program compares the entered character with all uppercase and lowercase vowels.
If no match is found, it is considered a consonant.
Concepts Covered
- if…else
- Logical OR Operator
- Character Comparison
- Decision Making
7. C++ Program to Check Whether a Number is Divisible by 5 and 11
Problem Statement
Write a C++ program to determine whether a number is divisible by both 5 and 11.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number: ";
cin >> number;
if (number % 5 == 0 && number % 11 == 0)
{
cout << "Number is divisible by both 5 and 11.";
}
else
{
cout << "Number is NOT divisible by both 5 and 11.";
}
return 0;
}
Sample Input
Enter a number: 110
Sample Output
Number is divisible by both 5 and 11.
Explanation
The logical AND (&&) operator ensures that both divisibility conditions are true before displaying the result.
Concepts Covered
- Logical AND
- Modulus Operator
- Multiple Conditions
8. C++ Program to Check Pass or Fail
Problem Statement
Write a C++ program to determine whether a student has passed or failed.
Passing Marks = 40
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int marks;
cout << "Enter marks: ";
cin >> marks;
if (marks >= 40)
{
cout << "Pass";
}
else
{
cout << "Fail";
}
return 0;
}
Sample Input
Enter marks: 76
Sample Output
Pass
Explanation
If the student’s marks are greater than or equal to 40, the student passes; otherwise, the student fails.
Concepts Covered
- if…else
- Comparison Operators
- Decision Making
9. C++ Program to Calculate Student Grade
Problem Statement
Write a C++ program to assign grades based on the following criteria:
| Marks | Grade |
|---|---|
| 90–100 | A |
| 80–89 | B |
| 70–79 | C |
| 60–69 | D |
| Below 60 | F |
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int marks;
cout << "Enter marks: ";
cin >> marks;
if (marks >= 90)
{
cout << "Grade A";
}
else if (marks >= 80)
{
cout << "Grade B";
}
else if (marks >= 70)
{
cout << "Grade C";
}
else if (marks >= 60)
{
cout << "Grade D";
}
else
{
cout << "Grade F";
}
return 0;
}
Sample Input
Enter marks: 84
Sample Output
Grade B
Explanation
The program evaluates the marks using an else-if ladder and assigns the appropriate grade.
Concepts Covered
- else-if Ladder
- Comparison Operators
- Decision Making
10. C++ Program to Check Whether a Character is an Alphabet, Digit, or Special Character
Problem Statement
Write a C++ program to determine whether an entered character is an alphabet, a digit, or a special character.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
char ch;
cout << "Enter a character: ";
cin >> ch;
if ((ch >= 'A' && ch <= 'Z') ||
(ch >= 'a' && ch <= 'z'))
{
cout << "Alphabet";
}
else if (ch >= '0' && ch <= '9')
{
cout << "Digit";
}
else
{
cout << "Special Character";
}
return 0;
}
Sample Input
Enter a character: @
Sample Output
Special Character
Explanation
The program compares the ASCII value of the entered character to determine its category.
A–Zora–z→ Alphabet0–9→ Digit- Anything else → Special Character
Concepts Covered
- ASCII Values
- Character Comparison
- if…else-if Ladder
- Logical Operators
11. C++ Program to Check Whether a Person is Eligible to Vote
Problem Statement
Write a C++ program to determine whether a person is eligible to vote.
Minimum Voting Age = 18 years
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 18)
{
cout << "Eligible to Vote";
}
else
{
cout << "Not Eligible to Vote";
}
return 0;
}
Sample Input
Enter your age: 20
Sample Output
Eligible to Vote
Explanation
The program checks whether the entered age is 18 or above. If the condition is true, the user is eligible to vote; otherwise, they are not.
Concepts Covered
- if…else
- Comparison Operators
- Decision Making
12. C++ Program to Find Whether a Number is Divisible by 3 or 7
Problem Statement
Write a C++ program to determine whether a number is divisible by 3 or 7.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number: ";
cin >> number;
if (number % 3 == 0 || number % 7 == 0)
{
cout << "Number is divisible by 3 or 7.";
}
else
{
cout << "Number is NOT divisible by 3 or 7.";
}
return 0;
}
Sample Input
Enter a number: 21
Sample Output
Number is divisible by 3 or 7.
Explanation
The logical OR (||) operator checks whether at least one of the conditions is true.
Concepts Covered
- Logical OR
- Modulus Operator
- Multiple Conditions
13. C++ Program to Calculate Electricity Bill Based on Units
Problem Statement
Write a C++ program to calculate the electricity bill using the following rates:
| Units | Rate |
|---|---|
| First 100 Units | ₹5/unit |
| Next 100 Units | ₹7/unit |
| Above 200 Units | ₹10/unit |
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int units;
float bill;
cout << "Enter electricity units: ";
cin >> units;
if (units <= 100)
{
bill = units * 5;
}
else if (units <= 200)
{
bill = (100 * 5) + ((units - 100) * 7);
}
else
{
bill = (100 * 5) + (100 * 7) + ((units - 200) * 10);
}
cout << "Electricity Bill = Rs. " << bill;
return 0;
}
Sample Input
Enter electricity units: 250
Sample Output
Electricity Bill = Rs. 1700
Explanation
The bill is calculated using different pricing slabs based on the number of units consumed.
Concepts Covered
- else-if Ladder
- Real-world Calculations
- Decision Making
- Arithmetic Operations
14. C++ Program to Find the Absolute Value of a Number
Problem Statement
Write a C++ program to find the absolute value of an integer.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number: ";
cin >> number;
if (number < 0)
{
number = -number;
}
cout << "Absolute Value = " << number;
return 0;
}
Sample Input
Enter a number: -45
Sample Output
Absolute Value = 45
Explanation
If the entered number is negative, it is multiplied by -1 to make it positive.
Concepts Covered
- if Statement
- Arithmetic Operators
- Integer Manipulation
15. C++ Program to Display the Day of the Week Using Switch Statement
Problem Statement
Write a C++ program to display the day of the week based on a number entered by the user.
| Number | Day |
|---|---|
| 1 | Monday |
| 2 | Tuesday |
| 3 | Wednesday |
| 4 | Thursday |
| 5 | Friday |
| 6 | Saturday |
| 7 | Sunday |
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int day;
cout << "Enter day number (1-7): ";
cin >> day;
switch(day)
{
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
default:
cout << "Invalid Day Number";
}
return 0;
}
Sample Input
Enter day number (1-7): 5
Sample Output
Friday
Explanation
The switch statement is used when there are multiple possible values for a single variable. Each case represents a different condition, and break prevents execution from continuing into the next case.
Concepts Covered
- switch Statement
- Multiple Cases
- break Statement
- default Case
Chapter Summary
In this chapter, you learned how conditional statements allow C++ programs to make decisions based on different conditions. You practiced using if, if...else, else if ladder, logical operators, comparison operators, and the switch statement to solve real-world problems such as checking voting eligibility, grading students, identifying vowels, calculating electricity bills, and displaying days of the week. These decision-making techniques are essential for building interactive and intelligent C++ applications.
Key Takeaways
- Conditional statements control the flow of a program.
ifis used for checking a single condition.if...elseis used when there are two possible outcomes.else ifladder is suitable for multiple conditions.switchis useful when checking one variable against multiple fixed values.- Logical operators (
&&,||,!) help combine conditions. - Comparison operators return either
trueorfalse. - Nested conditions help solve complex decision-making problems.
- Real-world applications frequently rely on conditional statements.
- Mastering conditional statements is essential before learning loops and functions.
Frequently Asked Questions (FAQs)
1. What are conditional statements in C++?
Conditional statements allow a program to execute different blocks of code depending on whether a condition is true or false.
2. What is the difference between if and switch?
ifcan evaluate any logical expression.switchcompares a single variable against multiple constant values.
3. When should I use an else if ladder?
Use an else if ladder when you need to check multiple conditions in sequence.
4. What is a nested if statement?
A nested if is an if statement placed inside another if statement to evaluate more specific conditions.
5. Why do we use logical operators?
Logical operators combine multiple conditions and are commonly used in decision-making.
6. What happens if no switch case matches?
The default case executes if none of the specified cases match the entered value.
7. Can switch be used with strings in C++?
No. Traditional C++ switch statements work only with integral types such as int, char, and enum.
8. Why are conditional statements important?
Conditional statements enable programs to make decisions, validate input, perform calculations based on rules, and create dynamic applications, making them one of the core building blocks of C++ programming.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
