Programming Exercise 4-2 Using if, else-if, and else statements, write a program
ID: 3756331 • Letter: P
Question
Programming Exercise 4-2
Using if, else-if, and else statements, write a program which prompts the user to enter their age, and then displays a different message depending on the age given.
age
message
less than 16
"You can't drive."
16 to 17
"You can drive but not vote."
18 to 24
"You can vote but not rent a car."
25 or older
"You can do pretty much anything."
age
message
less than 16
"You can't drive."
16 to 17
"You can drive but not vote."
18 to 24
"You can vote but not rent a car."
25 or older
"You can do pretty much anything."
Explanation / Answer
#include<iostream>
//you haven't specified which language it is
//i have answered it in cpp, with basic syntax
//this will help you with logic
//comment your required language, i will, provide the code for it
using namespace std;
int main()
{
int age;//integer to store age
//reading input
cout<<"Enter age:";
cin>>age;
//displaying message asper the age
if(age<16)
{
cout<<"You can't drive. ";
}
else if(age==16 || age==17)
{
cout<<"You can drive but not vote. ";
}
else if(age>=18 && age <=24)
{
cout<<"You can vote but not rent a car. ";
}
else//25 or older than 25
{
cout<<"You can do pretty much anything. ";
}
return 0;
}
/*
output1:
Enter age:14
You can't drive.
Process exited normally.
Press any key to continue . . .
output2:
Enter age:16
You can drive but not vote.
Process exited normally.
Press any key to continue . . .
output3:
Enter age:19
You can vote but not rent a car.
Process exited normally.
Press any key to continue . . .
output4:
Enter age:58
You can do pretty much anything.
Process exited normally.
Press any key to continue . . .
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.