20 IF–ELSE Statement Questions in C++ with Answers (Beginner Friendly)

 



Introduction

The if–else statement is one of the most important decision-making statements in C++ programming.
It allows a program to execute different blocks of code based on a condition.

If you are a beginner, preparing for:

  • College exams

  • Coding interviews

  • Competitive programming

then practicing if–else questions is very important.

👉 In this article, you will learn 20 important if–else statement questions in C++ with answers and examples.


What is IF–ELSE Statement in C++?

The if–else statement is used to check conditions.

Syntax:

if(condition) { // code if condition is true } else { // code if condition is false }

20 Important IF–ELSE Statement Questions in C++


1. Program to check whether a number is even or odd

int n = 10; if(n % 2 == 0) cout << "Even"; else cout << "Odd";

Output: Even


2. Check whether a number is positive, negative or zero

int n = -5; if(n > 0) cout << "Positive"; else if(n < 0) cout << "Negative"; else cout << "Zero";

Output: Negative


3. Find the greatest of two numbers

int a = 10, b = 20; if(a > b) cout << a; else cout << b;

Output: 20


4. Find the greatest of three numbers

int a = 5, b = 9, c = 3; if(a > b && a > c) cout << a; else if(b > c) cout << b; else cout << c;

Output: 9


5. Check voting eligibility

int age = 17; if(age >= 18) cout << "Eligible for Voting"; else cout << "Not Eligible";

Output: Not Eligible


6. Check pass or fail

int marks = 35; if(marks >= 40) cout << "Pass"; else cout << "Fail";

Output: Fail


7. Program to check leap year

int year = 2024; if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) cout << "Leap Year"; else cout << "Not a Leap Year";

Output: Leap Year


8. Check whether a character is vowel or consonant

char ch = 'a'; if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') cout << "Vowel"; else cout << "Consonant";

Output: Vowel


9. Check whether a number is divisible by 5

int n = 25; if(n % 5 == 0) cout << "Divisible by 5"; else cout << "Not Divisible by 5";

Output: Divisible by 5


10. Check profit or loss

int cp = 100, sp = 80; if(sp > cp) cout << "Profit"; else if(sp < cp) cout << "Loss"; else cout << "No Profit No Loss";

Output: Loss


11. Check whether a character is an alphabet

char ch = '9'; if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) cout << "Alphabet"; else cout << "Not an Alphabet";

Output: Not an Alphabet


12. Check whether a number is zero

int n = 0; if(n == 0) cout << "Zero"; else cout << "Non-Zero";

Output: Zero


13. Check temperature condition

int temp = 30; if(temp > 25) cout << "Hot Weather"; else cout << "Cold Weather";

Output: Hot Weather


14. Check eligibility for driving license

int age = 19; if(age >= 18) cout << "Eligible for Driving License"; else cout << "Not Eligible";

Output: Eligible for Driving License


15. Check whether number is divisible by 3 and 7

int n = 21; if(n % 3 == 0 && n % 7 == 0) cout << "Divisible by 3 and 7"; else cout << "Not Divisible";

Output: Divisible by 3 and 7


16. Program to find grade

int marks = 85; if(marks >= 90) cout << "Grade A"; else if(marks >= 75) cout << "Grade B"; else if(marks >= 50) cout << "Grade C"; else cout << "Fail";

Output: Grade B


17. Check whether a character is uppercase

char ch = 'G'; if(ch >= 'A' && ch <= 'Z') cout << "Uppercase Letter"; else cout << "Not Uppercase";

Output: Uppercase Letter


18. Check bonus eligibility

int salary = 25000; if(salary >= 30000) cout << "Bonus Available"; else cout << "No Bonus";

Output: No Bonus


19. Check whether number is a 3-digit number

int n = 345; if(n >= 100 && n <= 999) cout << "Three Digit Number"; else cout << "Not a Three Digit Number";

Output: Three Digit Number


20. Simple login validation

int pin = 1234; if(pin == 1234) cout << "Access Granted"; else cout << "Access Denied";

Output: Access Granted


Advantages of IF–ELSE Statement

  • Easy to understand

  • Controls program flow

  • Used in real-life decision making

  • Very important for interviews


Conclusion

The if–else statement in C++ is a core concept that every beginner must master.
Practicing these 20 if–else questions with answers will help you build logic and confidence.

Comments

Popular posts from this blog

C++ Programming for Beginners – Step by Step Guide

If-Else Statement in C++

Variables and Data Types in C++