C++ program that prompts the user to input an integer between 0 and 35. If the n
ID: 3855363 • Letter: C
Question
C++ program that prompts the user to input an integer between 0 and 35. If the number is less than or equal to 9, the program should output the number; otherwise, it should output A for 10, B for 11, C for 12 ...and Z for 35. (Hint: Use the cast operator, static_cast<char>(), for numbers >=10.)
OUTPUT:
**********************************************************************
Enter an Integer Between 0 and 35: 8
8
**********************************************************************
Enter an Integer Between 0 and 35: 15
F
**********************************************************************
Enter an Integer Between 0 and 35: 40
Invalid Input
**********************************************************************
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter an Integer Between 0 and 35: ";
cin >> n;
if(n >=0 && n <=9) {
cout<<n<<endl;
} else if(n>=10 && n<=35) {
cout<<static_cast<char>(n - 10 + 'A')<<endl;
} else {
cout<<"Invalid Input"<<endl;
}
return 0;
}
Output:
sh-4.2$ g++ -std=c++11 -o main *.cpp
sh-4.2$ main
Enter an Integer Between 0 and 35: 8
8
sh-4.2$ main
Enter an Integer Between 0 and 35: 15
F
sh-4.2$ main
Enter an Integer Between 0 and 35: 40
Invalid Input
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.