Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

LAB 5 – C++ CONTROL STRUCTURES-I

Learning Objectives
In today’s lab, you will practice;
1. Using one-way selection structure (if).
2. Using two-way selection structure (if/else).
3. Using multiple selection structure (switch).

Practice Questions
You can practice the following problems in this lab.
Task 1.1 Increment and Decrement Operators
Write a C++ program which prompts the user to enter an integer value, stores it into a variable
called ‘num’, evaluates the following expressions and displays results on screen.
num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3))
For performing addition and subtraction, you are allowed to use ONLY the increment and
decrement operators (both prefixing and postfixing are allowed). You must remember that
using increment/decrement operators changes the original value of a number. Indent your code
and include comments for improving the readability of your code. Your program should have
the following interface.

Enter a number : 20
20 + 5 = 25
20 – 3 = 17
(20 + 3)- 2 = 21
((20 + 5)*2 / (20 + 3)) = 2

Task 1.2 ‘if’ Statement


Write a C++ program which takes two numbers from the user and tells which number is greater.
Your output should be as below.

Enter the first number : 25


Enter the second number : 32
First number is 25 and second number is 32.
Second number is greater.

Task 1.3 ‘if/else’ Statement


Write a C++ program which takes five numbers from the user and finds the largest of these
numbers. Your output should be as below.

Enter the first number : 25


Enter the second number : 32
Enter the third number : 12
Enter the fourth number : 85
Enter the fifth number : 53
The largest of the 5 numbers is 85.
Task 1.4 if/else’ Statement
Write a C++ program which generates Grades for students depending on their Scores. Here is
the criterion for awarding Grades.
• If Score is between 85 -100, Grade is ‘A’
• If Score is between 72 - 84, Grade is ‘B’
• If Score is between 60 - 71, Grade is ‘C’
• If Score is between 50 - 59, Grade is ‘D’
• If Score is between 0 - 49, Grade is ‘F’ (‘F’ Grade indicates that the student has failed.)
For all other values of Score entered by user, your program should print “Invalid Score was
entered. No Grade can be generated”. The output of your program should be as below.

Enter student’s score : 67


The student passed in C Grade.

Enter student’s score : 43


The student has failed with F Grade.

Task 1.5 ‘switch’ Statement


Repeat Task 1.4 using ‘switch’ statement.

Task 1.6 ‘if/else’ Statement


Write a C++ program which takes a character input from user and tells if it is a digit or not. If
it is a digit, your program should tell if it is even or odd. If it is not a digit, your program should
tell if it is an alphabet or not. If it is an alphabet, your program should tell if it is uppercase or
lowercase. If it is not an alphabet, your program should print “It is a special symbol”. The
output of your program should be as below.

Enter a value : 7
It is an odd digit.

Enter a value : H
It is an alphabet in UPPERCASE.

Enter a value : f
It is an alphabet in lowercase.

Enter a value : *
It is a special symbol.

You might also like