Write a program that prompts the user to input an integer between 0 and 35. The prompt should say Enter 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: For numbers >= 10, calculate the ASCII value for the corresponding letter and convert it to a char using the cast operator, static_cast().)

Respuesta :

Answer:

The code for answer this written in C++ is given below:

#include <iostream>

int main() {

 int number;

 std::cout << "Enter an integer between 0 and 35 \n";

 std::cin >> number;

 if(number >= 0 && number < 10){

   std::cout << "The ASCII is: " << number << "\n";

 }else if(number >= 10 && number <= 35){

   std::cout << "The ASCI is: " << static_cast<char>(number + 55) << "\n";

 }else{

   std::cout << "Enter a number between 0 and 35!";

 }

}

Explanation:

The explanation of each line in the code is given below:

#include <iostream> //The library for use input and output flow, cout - cin.

int main() { //Start the logic of the program

 int number; // Declare a variable which is going to store the input of the user.

 std::cout << "Enter an integer between 0 and 35 \n";

/* Show in the console the message to request a number between 0 and 35*/

 std::cin >> number;

/* Store the input of the user in the number variable.*/

 if(number >= 0 && number < 10){

//If the number is between 0 and 10 print the number.

   std::cout << "The ASCII is: " << number << "\n";

 }else if(number >= 10 && number <= 35){

/*If the number is between 10 and 35 convert to ASCII, add 55 to the number because the letters in ASCII representation starts in 65*/

   std::cout << "The ASCI is: " << static_cast<char>(number + 55) << "\n";

 }else{

/*If the number is not between 0 and 35 display an error message*/

   std::cout << "Enter a number between 0 and 35!";

 }

}

ACCESS MORE
EDU ACCESS
Universidad de Mexico