In: Computer Science
Write a program and ask a user to enter a numeric value between 0 - 99. Your program will spell out the numbers into words. You must use at least three switch cases in addition to multiple if statements. Each missing switch statement will reduce your grade for this problem by 20%. Note: The total number of if statements and switch cases should not exceed 28. Additional if/case statement will further reduce your grade by 5%.
in c++ please
#include<iostream>
using namespace std;
int main()
{
int num,tens_digit=-1,units_digit=-1;//decalring variables for number ,stroing unita and tens digit of number
string units="",tens="";//strings for units and tens digit
cout<<"Enter the number from 0 - 99"<<endl;
cin>>num;//reading number
units_digit=num%10;//retrieving units digit of the number
num=num/10;
tens_digit=num%10;
//retrieving units digit of the number
if(tens_digit==1 )//(1st if)if the number is between 10 to 19 this if condition is true.
{
switch(units_digit)//(contains 10 cases 10+1 if = 11)
{
case 0: cout<<"Ten";
break;
case 1: cout<<" Eleven";
break;
case 2: cout<<"Twelve";
break;
case 3: cout<<"Thirteen";
break;
case 4: cout<<" Fourteen";
break;
case 5: cout<<" Fifteen";
break;
case 6: cout<<" Sixteen";
break;
case 7: cout<<" Seventeen";
break;
case 8: cout<<" Eighteen";
break;
case 9: cout<<" Ninteen";
break;
}
return 0;// here the program will end if number is between 10 to 19 because there is no need to check for other switch statements.
}
switch(tens_digit) //(contains 8 cases 11+8=19)used to determine the value for tens digit
{
case 2: tens="Twenty";
break;
case 3: tens="Thirty";
break;
case 4: tens="Fourty";
break;
case 5: tens="Fifty";
break;
case 6: tens="Sixty";
break;
case 7: tens="Seventy";
break;
case 8: tens="Eighty";
break;
case 9: tens="Ninety";
break;
default: units="Zero";//used to print 0
}
switch(units_digit)//(contains 9 cases 19+9=28)used to determine the value for units digit
{
case 1: units="One";
break;
case 2: units="Two";
break;
case 3: units="Three";
break;
case 4: units="Four";
break;
case 5: units="Five";
break;
case 6: units="Six";
break;
case 7: units="Seven";
break;
case 8: units="Eight";
break;
case 9: units="Nine";
break;
}
cout<<tens << units;
return 0;
}
Please comment if you have any doubts.
Rate please!!!!