*** USING C++ *** Topics Branching switch statement Description Write a program
ID: 3805020 • Letter: #
Question
*** USING C++ ***
Topics
Branching
switch statement
Description
Write a program that translates a letter used in a telephone number into an equivalent numeric digit. The program asks the user to enter a letter, translates the letter into the corresponding digit and displays it. The user may enter its selection either as an upper or lower case letter.
The program translates the letter into a digit according to the table below:
Letters Digit
a b c 2
d e f 3
g h i 4
j k l 5
m n o 6
p q r s 7
t u v 8
w x y z 9
Testing
Input Test 1
Enter a letter: p
Output Test 1
Letter: p
Digit: 7
Input Test 2
Enter a letter: P
Output Test 1
Letter: P
Digit: 7
Sample Code
/*The code below determines the character entered by user.*/
/*
C++ code:
*/
char letter;
cout << “Enter a single alphabet:” << endl;
cin >> letter;
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int a[] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
cout<<"Enter a letter:";
char ch,ch1;
cin>>ch;
ch1=ch;
if(ch>='A' && ch<='Z')
{
ch1 = ch-'A'+'a';
}
cout<<"Letter: "<<ch<<endl;
cout<<"Digit: "<<a[ch1-'a']<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.