Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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>
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;
}
3
d) Repeat c) using a single if-else statement and an appropriate logical operator.