0% found this document useful (0 votes)
138 views4 pages

2

This document provides sample C++ code solutions to review questions about conditional statements. The questions cover: 1) Writing programs that check for height, age discounts, secret codes, and even/odd numbers using if/else statements. 2) Checking if a student is accepted to a master's program based on their GPA and absence rate, using nested if, single if with logical operators, if-else-if, and single if-else with logical operators.

Uploaded by

Ray Al
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views4 pages

2

This document provides sample C++ code solutions to review questions about conditional statements. The questions cover: 1) Writing programs that check for height, age discounts, secret codes, and even/odd numbers using if/else statements. 2) Checking if a student is accepted to a master's program based on their GPA and absence rate, using nested if, single if with logical operators, if-else-if, and single if-else with logical operators.

Uploaded by

Ray Al
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

College of Engineering

Introduction to Computing ENGR 112

Review Questions -3 : Solution


Question-1: Write a C++ program for each of the following problems. You are recommended to do the
analysis (what are the input, output, and the process) before the coding.

a) Write a program that asks the user to enter his height and displays the message “ Tall” if
the value entered is above 170 cm.

b) A travel company offers a 30% and 10% discount for passengers which ages are below 10
and above 60, respectively. Write a program that reads the age and displays the
appropriate message for each category of passengers.
double age;
cout << "\n Enter your age:";
cin >> age ;
if (age < 10)
cout << " \n Discount 30%" << endl ;
if (age > 60)
cout << " \n Discount 10%" << endl ;

c) A computer controlling a gate works as follows: It prompts the user to enter a code using a
keyboard. If the code matches the secret code (123), it displays the message “Welcome,
please get in”, if not it displays “Invalid code, try again”.

1
#include <iostream>

using namespace std ;

const int secret_code = 123 ;


int main()
{
int code;
cout << "\n Enter your code:";
cin >> code ;
if (code == secret_code)
cout << " \n Welcome please get in" << endl ;
else
cout << " \n invalid code , try again " << endl ;
return 0;
}

d) Write a program that read a number then displays “Even” if it is even “Odd” if it is odd.
Hint: use the modulus operator (%), by checking the value of n%2, where n is the entered
number.

int num;
cout << "\n enter number:";
cin >> num ;
if (num%2==0)
cout << " \n even number" << endl ;
else
cout << " \n odd number" << endl ;
return 0;

Question-2: A University accepts a student in a Master program if his GPA is above 3.5 and his total
accumulated absence does not exceed 20%.

a) Using two nested if write the piece of C++ code that reads the GPA and the Absence rate
then displays a message “Accepted” if he is eligible.
const float gpa_min = 3.5 ;
const int absence_rate_max = 20 ;
int main()
{
float gpa ;
int absence_rate ;

2
cout << " \n Enter GPA and Absence rate: " ;
cin >> gpa >> absence_rate ;
if(gpa > gpa_min)
if(absence_rate <= absence_rate_max)
cout << "\n Accepted " << endl ;
return 0;
}

b) Repeat a) using a single if and an appropriate logical operator.


const float gpa_min = 3.5 ;
const int absence_rate_max = 20 ;
int main()
{
float gpa ;
int absence_rate ;
cout << " \n Enter GPA and Absence rate: " ;
cin >> gpa >> absence_rate ;
if(gpa > gpa_min && absence_rate <= absence_rate_max)
cout << "\n Accepted in Master Program" ;
return 0;
}
c) Using if-else-if statement write the piece of C++ code that reads the GPA and the Absence
rate then displays a message “Accepted” if he is eligible, “Not accepted” otherwise
const float gpa_min = 3.5 ;
const int absence_rate_max = 20 ;
int main()
{
float gpa ;
int absence_rate ;
cout << " \n Enter GPA and Absence rate: " ;
cin >> gpa >> absence_rate ;
if(gpa <= gpa_min)
cout << "\n Not accepted in Master Program" << endl ;
else
{
if (absence_rate <= absence_rate_max)
cout << "\n Accepted in Master Program" << endl ;
else
cout << "\n Not accepted in Master Program" << endl ;
}
}

3
d) Repeat c) using a single if-else statement and an appropriate logical operator.

const float gpa_min = 3.5 ;


const int absence_rate_max = 20 ;
int main()
{
float gpa ;
int absence_rate ;
cout << " \n Enter GPA and Absence rate: " ;
cin >> gpa >> absence_rate ;
if(gpa > gpa_min && absence_rate <= absence_rate_max )
cout << "\n Accepted in Master Program" << endl ;
else
cout << "\n Not accepted in Master Program" << endl ;
}

You might also like