08 Practice Conditional Structure
08 Practice Conditional Structure
Objective:
• To get a grip on Conditional/Selection Structure.
• Issues related to keyboard input stream.
Selection Structure
Task-1:
The starting point for the selection structure in C++ is your practice file-2, Your previous quizzes/labs
based on decision structure.
Task-2:
Write a program that can be used as a math tutor for a young student. The program should display
two random numbers to be added, such as
247
+ 129
---------
The program should then ask student/user to enter the answer. After getting answer from user, your
program checks whether the answer is correct or not and display the message accordingly
Sample Run:
*****Kangaroo Math Competition*****
247
+ 129
--------
Hey Kido! Enter Your Answer: 312
OOPs Kido! Your answer is incorrect. Keep doing the hard word. You will crack it one day.
247
+ 129
--------
376
Note: Both random numbers will be greater than zero and maximum 5 digits. The input/output should
be displayed just as shown in the sample run.
Task-3:
A long-distance carrier charges the following rates for telephone calls:
Write a program that asks for the starting time and the number of minutes of the call, and displays the
charges. The program should ask for the time to be entered as a floating-point number in the form
HH.MM. For example, 07:00 hours will be entered as 07.00, and 16:28 hours will be entered as 16.28.
Input Validation: The program should not accept a time value, which is greater than 23:59.
Also, no number whose last two digits are greater than 59 should be accepted.
Hint:
Assuming num is a floating-point variable, the following expression will give you its fractional part:
num - (int) num
Task-2:
Suppose x and y are int variables and z is a double variable. Assume the following input data:
37 86.56 32
What value (if any) is assigned to x, y, and z after each of the following statements executes? (Use the
same input for each statement.)
A. cin >> x >> y >> z;
B. cin >> x >> z >> y;
C. cin >> z >> x >> y;
Task-3:
Suppose x and y are int variables and ch is a char variable. Assume the following input data:
13 28 D
14 E 98
A B 56
What value (if any) is assigned to x, y, and ch after each of the following statements executes? (Use
the same input for each statement.
A. cin >> x >> y;
cin.ignore(50, '\n');
cin >> ch;
B. cin >> x;
cin.ignore(50, '\n');
cin >> y;
cin.ignore(50, '\n');
cin.get(ch);
C. cin >> y;
cin.ignore(50, '\n');
cin >> x >> ch;
D. cin.get(ch);
cin.ignore(50, '\n');
cin >> x;
cin.ignore(50, 'E');
cin >> y;
Task-4:
Given the input:
46 A 49
and the C++ code:
OR the input is
B. 23
Lance Grant
Task-1:
Given three numbers a, b and c such that a, b and c can be at most 10#$ . The task is to compute (𝑎 ∗
𝑏)%𝑐.
Task-2:
Write a program which inputs an integer representing the weekday (1, 2, …, 7), and display on console
the day of week in English (Monday, Tuesday, …, Sunday).
Input Validation: If user enters value other than 1 to 7 then your program should display a message
“Not a valid Week Day”.
Note: Do this Task using switch statement
For Example:
• If user enters 1 then your program display Monday.
• If user enters 3 then your program display Wednesday.
• If user enters -4 then your program display Not a Valid Week Day.
Task-3:
Write a program that can be used as a math tutor for a young student. The program should display
two random numbers to be added, or subtracted, divided or, multiplied. It is an extension to the Task-
2 of this practice file. In which your program will randomly decide the type of operation
(add/divided/multiply/subtract/Mod).
Note: In case of any operation, both the operands signs will be chosen randomly.
Example Inputs:
Task-4: ABRACADABRA
In this task, our goal is to develop a program, which should be able to guess a number in user’s mind.
This program will ask the user to think of any number in the range of 1 ~ 1000.
Your program should be sharp / intelligent enough to guess the number in user’s mind in not more
than 10 unsuccessful attempts. At each attempt your program can ask the user the following queries:
Assume your program guess a number ‘X’, then the program will ask from user:
§ Is the hidden number equal to ‘X’.
§ Is the hidden number less than ‘X’.
§ Is the hidden number greater than ‘X’.
Write a program that calculates a customer’s monthly bill. It should ask which package the customer
has purchased and how many hours were used. It should then display the total amount due.
Input Validation: Be sure the user only selects package A, B, or C. Also, the number of hours used in a
month cannot exceed 744.