Sei sulla pagina 1di 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 ;
}

Potrebbero piacerti anche