C++ Coding Exercises - Conditional Structures
Practice C++ programming with coding exercises covering variables, data types, operators, input and output, type casting, conditional statements, loops, functions, arrays, and other C++ fundamentals.
if Statements
Problem Statement 1: Positive, Negative or Zero - if statement
Write a C++ program that takes a number as input and checks whether it is positive, negative, or zero. Display the appropriate message based on the entered number.
Hint: Use an if-else-if ladder.
Problem Statement 2: Check Even or Odd - if statement
Write a C++ program that takes an integer as input and determines whether it is even or odd. Display an appropriate message based on the result.
Hint: Use the modulus operator (%) to check the remainder when the number is divided by 2.
Problem Statement 3: Check Eligibility Based on Marks - if statement
Write a C++ program that takes a student's marks as input. If the marks are 50 or above, display "Eligible"; otherwise, display "Not Eligible".
Hint: Use an if-else statement to check the marks.
Problem Statement 4: Student Grade Calculator - if statement
Write a C++ program that takes a student's marks as input and displays the corresponding grade according to the following criteria:
- A: 80–100 marks
- B: 70–79 marks
- C: 60–69 marks
- D: 50–59 marks
- Fail: Below 50 marks
If the entered marks are less than 0 or greater than 100, display "Invalid Marks".
Hint: Use an if-else-if ladder to check the marks and assign the appropriate grade. First, validate that the marks are between 0 and 100.
Problem Statement 5: Find the Larger Number - if statement
Write a C++ program that takes two numbers as input and compares them. Display the larger number. If both numbers are equal, display "Both numbers are equal."
Hint: Use an if-else-if ladder to compare the two numbers and handle all three possible cases.
Problem Statement 6: Login Authentication System - if statement
Write a C++ program that takes two inputs: a user verification flag and a password verification flag. Display "Login Successful" only if both flags indicate that the username and password are correct. Otherwise, display "Login Failed".
Hint: Use nested if statements to check the user verification flag first and then the password verification flag.
Problem Statement 7: Scholarship Eligibility Checker - if statement
Write a C++ program that takes a student's marks and family monthly income as input. The student is eligible for a scholarship only if both of the following conditions are met:
- The student's marks are 80 or above.
- The family's monthly income is below Rs. 60,000.
Display "Scholarship Awarded" if the student meets both conditions. Otherwise, display "Not Eligible for Scholarship".
Hint: Use nested if statements to check the marks first and then the family income.
Course Registration
Problem Statement 8: Student Registration Eligibility - if statement
Write a C++ program that takes three inputs to check whether a student can register:
- Admission Status: Admission is confirmed.
- Fee Status: Required fee has been paid.
- Document Verification Status: All required documents have been verified.
Display "Registration Allowed" only if all three requirements are satisfied. Otherwise, display "Registration Not Allowed".
Hint: Use nested if statements with appropriate else branches to check each requirement one by one.
Problem Statement 9: Driving Eligibility (&& — AND)
Problem Statement
Write a C++ program to determine whether a person is eligible to drive. A person must be at least 18 years old and have a valid driving licence.
Requirements
- Input the person's age and whether they have a valid driving licence.
- Use the logical AND operator (
&&) in anif-elsestatement. - Display
Allowed to driveif both conditions are true; otherwise, displayNot allowed to drive. - Test the program with different ages and licence statuses.
Problem Statement 10: Examination Hall Entry (|| — OR)
Problem Statement
Write a C++ program to determine whether a student is allowed to enter an examination hall. A student may enter if they have a valid admit card or special permission from the examination authorities.
Requirements
- Input whether the student has a valid admit card and whether they have special permission.
- Use the logical OR operator (
||) in anif-elsestatement. - Display
Entry allowedif at least one condition is true; otherwise, displayEntry denied. - Test all four combinations of admit card and special permission statuses.
Problem Statement 11: Free Delivery (&& — AND)
Problem Statement
Write a C++ program to determine whether a customer qualifies for free delivery. Free delivery is available only when the order amount is Rs. 3,000 or more and the customer's address is within the delivery area.
Requirements
- Input the order amount and whether the customer's address is within the delivery area.
- Use the logical AND operator (
&&) in anif-elsestatement. - Display
Free deliveryif both conditions are true; otherwise, displayDelivery charges apply. - Test the program with order amounts below, equal to, and above Rs. 3,000, using both delivery-area statuses.
Problem Statement 12: Library Account Status (! — NOT)
Problem Statement
Write a C++ program to determine whether a student's library account is active or blocked. Use a Boolean variable named isBlocked to indicate whether the student's account has been blocked.
Requirements
- Declare a Boolean variable named
isBlocked. - Assign
trueif the account is blocked andfalseif the account is not blocked. - Use the logical NOT operator (
!) in anif-elsestatement to check the account status. - Display
Account activeif the account is not blocked; otherwise, displayAccount blocked. - Test the program with both
trueandfalsevalues.
Problem Statement 13: University Admission Eligibility (&& and || — Mixed Operators)
Problem Statement
Write a C++ program to determine whether a student is eligible for university admission. The student must score at least 60 marks and must have either a valid entry test result or an approved entry test exemption.
Requirements
- Input the student's marks, whether their entry test result is valid, and whether an exemption has been approved.
- Use the logical AND (
&&) and OR (||) operators together in anif-elsestatement. - Display
Eligible for admissionif the student scores at least 60 marks and either has a valid entry test result or an approved exemption. - Otherwise, display
Not eligible. - Test at least four combinations, including:
- Marks below 60 with a valid entry test result.
- Marks of 60 or above with a valid entry test result.
- Marks of 60 or above with an approved exemption but no valid entry test result.
- Marks of 60 or above with neither a valid entry test result nor an approved exemption.
switch Statement
Problem Statement 14: Day Type Checker Using switch
Write a C++ program that asks the user to enter a day number from 1 to 7. Use a switch statement to determine whether the entered day is a Weekday or a Weekend.
- If the user enters a number from 1 to 5, display
Weekday. - If the user enters 6 or 7, display
Weekend. - If the user enters any number outside the range of 1 to 7, display
Invalid day number.
Example Output 1:
Enter day number (1-7): 3
Weekday
Example Output 2:
Enter day number (1-7): 7
Weekend
Example Output 3:
Enter day number (1-7): 9
Invalid day number
Problem Statement 15: Simple Calculator Using switch
Write a C++ program that asks the user to enter two numbers of type double and an arithmetic operator (+, -, *, /, or %). Use a switch statement to perform the selected arithmetic operation and display the result.
Requirements
- If the operator is
+, add the two numbers. - If the operator is
-, subtract the second number from the first. - If the operator is
*, multiply the two numbers. - If the operator is
/, divide the first number by the second. If the second number is zero, displayCannot divide by zero. - If the operator is
%, convert both numbers to integers and calculate the remainder. If the converted second number is zero, displayCannot calculate remainder by zero. - If the user enters an invalid operator, display
Invalid operator.
Example Output 1: Addition
Enter first number: 10.5
Enter operator (+, -, *, /, %): +
Enter second number: 5.2
Result = 15.7Example Output 2: Decimal Division
Enter first number: 10
Enter operator (+, -, *, /, %): /
Enter second number: 4
Result = 2.5Example Output 3: Division by Zero
Enter first number: 10
Enter operator (+, -, *, /, %): /
Enter second number: 0
Cannot divide by zeroExample Output 4: Modulus
Enter first number: 10
Enter operator (+, -, *, /, %): %
Enter second number: 3
Result = 1Example Output 5: Invalid Operator
Enter first number: 10
Enter operator (+, -, *, /, %): &
Enter second number: 5
Invalid operatorNote: Use double variables for both inputs. The modulus (%) operator works only with integers in C++, so convert the operands to integers for this operation. Any decimal parts will be discarded during conversion.
Problem Statement 16: Vowel Checker Using switch
Write a C++ program that asks the user to enter a single character. Use a switch statement to check whether the entered character is a vowel (a, e, i, o, or u). The program should recognize both lowercase and uppercase vowels.
Requirements
- If the character is
a,e,i,o, oru, displayVowel. - If the character is
A,E,I,O, orU, displayVowel. - For any other character, display
Not a vowel.
Example Output 1: Lowercase Vowel
Enter a character: e
VowelExample Output 2: Uppercase Vowel
Enter a character: U
VowelExample Output 3: Consonant
Enter a character: b
Not a vowelExample Output 4: Digit
Enter a character: 5
Not a vowelProblem Statement 17: Student Management System Using switch
Write a C++ program to display a menu for a simple Student Management System. Ask the user to enter a choice from the menu and use a switch statement to perform the corresponding action.
Requirements
Display the following menu:
- Student Profile
- Semester Result
- Fee Status
- Exit
- If the user enters
1, displayDisplaying student profile. - If the user enters
2, displayDisplaying semester result. - If the user enters
3, displayDisplaying fee status. - If the user enters
4, displayExiting the program. - If the user enters any other number, display
Invalid choice.
Example Output 1: Student Profile
--- Student Management System ---
1. Student Profile
2. Semester Result
3. Fee Status
4. Exit
Enter your choice: 1
Displaying student profile.Example Output 2: Fee Status
--- Student Management System ---
1. Student Profile
2. Semester Result
3. Fee Status
4. Exit
Enter your choice: 3
Displaying fee status.Example Output 3: Invalid Choice
--- Student Management System ---
1. Student Profile
2. Semester Result
3. Fee Status
4. Exit
Enter your choice: 5
Invalid choice.Problem Statement 18: Grade Evaluation Using switch
Write a C++ program that asks the user to enter a letter grade (A, B, C, D, or F). Use a switch statement to display the corresponding performance message.
Requirements
- If the grade is
A, displayExcellent Performance. - If the grade is
B, displayVery Good Performance. - If the grade is
C, displayGood Performance. - If the grade is
D, displayYou Passed. - If the grade is
F, displayYou Failed. - If the user enters any other character, display
Invalid grade. - Modify the program to accept both uppercase and lowercase grades (e.g.,
Aanda). - Add grade
Eand displayNeeds Improvement. - Test the program with valid and invalid grades to verify the output.
Sample Output 1
Enter your grade (A, B, C, D, F): A
Excellent PerformanceSample Output 2
Enter your grade (A, B, C, D, F): b
Very Good PerformanceSample Output 3
Enter your grade (A, B, C, D, F): X
Invalid gradeProblem Statement 19: Number of Days in a Month Using switch
Write a C++ program that asks the user to enter a month number (1–12). Use a switch statement to display the number of days in the corresponding month.
Requirements
- If the user enters
1, display31 daysfor January. - If the user enters
2, display28 daysfor February. - If the user enters
3, display31 daysfor March. - If the user enters
4, display30 daysfor April. - If the user enters
5, display31 daysfor May. - If the user enters
6, display30 daysfor June. - If the user enters
7, display31 daysfor July. - If the user enters
8, display31 daysfor August. - If the user enters
9, display30 daysfor September. - If the user enters
10, display31 daysfor October. - If the user enters
11, display30 daysfor November. - If the user enters
12, display31 daysfor December. - If the user enters a number outside the range of 1–12, display
Invalid month number.
Note: Assume February has 28 days. Leap years are not considered.
Sample Output 1
Enter month number (1-12): 2
February has 28 days.Sample Output 2
Enter month number (1-12): 4
April has 30 days.Sample Output 3
Enter month number (1-12): 15
Invalid month numberProblem Statement 20: ATM Menu System Using switch
Difficulty Level: Advanced
Write a C++ program that simulates a simple ATM system. Display a menu that allows the user to check their account balance, deposit money, withdraw money, or exit the program. Use a switch statement to process the user's selection.
Requirements
- Balance Inquiry: Display the current account balance.
- Deposit: Ask the user to enter an amount and add it to the account balance. The amount must be greater than zero.
- Withdrawal: Ask the user to enter an amount and subtract it from the account balance. The amount must be greater than zero and must not exceed the available balance.
- Exit: Display
Thank you for using the ATM.and terminate the program. - Invalid Choice: If the user enters an invalid menu option, display
Invalid choice. Please try again. - Initialize the account balance with a starting amount of Rs. 10,000.
- Display the menu again after each transaction until the user selects Exit.
Sample Output 1: Balance Inquiry
--- ATM Menu ---
1. Balance Inquiry
2. Deposit
3. Withdrawal
4. Exit
Enter your choice: 1
Current Balance: Rs. 10000Sample Output 2: Deposit
Enter your choice: 2
Enter deposit amount: 2000
Deposit successful.
Updated Balance: Rs. 12000Sample Output 3: Withdrawal
Enter your choice: 3
Enter withdrawal amount: 3000
Withdrawal successful.
Updated Balance: Rs. 9000Sample Output 4: Insufficient Balance
Enter your choice: 3
Enter withdrawal amount: 15000
Insufficient balance.Sample Output 5: Exit
Enter your choice: 4
Thank you for using the ATM.