In: Mechanical Engineering
C++ Primary U.S. interstate highways are numbered 1-99. Odd numbers (like the 5 or 95) go north/south, and evens (like the 10 or 90) go east/west. Auxiliary highways are numbered 100-999, and service the primary highway indicated by the rightmost two digits. Thus, the 405 services the 5, and the 290 services the 90.
Your program will prompt the user to type a highway number ("Please enter a highway number, Ex: 65: "), read in the number typed by the user, and print out whether it is a primary or auxiliary highway. If auxiliary, indicate what primary highway it serves.
Also your program must indicate if the (primary) highway runs north/south or east/west, that it is an invalid number if not between 0 and 999.
Code for the same is attached below,please do go through it.
int main() {
int highwayNumber;
int primaryNumber;
cout<<"Please enter a highway number: ";
cin >> highwayNumber;
if (highwayNumber >= 1 && highwayNumber <=
999) {
if (highwayNumber <= 99) {
if (highwayNumber % 2 == 0) {
cout << "I-" << highwayNumber << " is primary,
going east/west." << endl;
} else {
cout << "I-" << highwayNumber << " is primary,
going north/south." << endl;
}
} else {
primaryNumber = highwayNumber;
highwayNumber %= 100;
if (highwayNumber % 2 == 0) {
cout << "I-" << primaryNumber << " is auxiliary,
serving I-" << highwayNumber << ", going east/west."
<< endl;
} else {
cout << "I-" << primaryNumber << " is auxiliary,
serving I-" << highwayNumber << ", north/south."
<< endl;
}
}
} else {
cout << highwayNumber << " is not a valid interstate
highway number." << endl;
}
return 0;
}
Input and Output in C++ Complier is attached below.
Please do hit that like button if my answers were of any help to you.
Thank you.