69 668
Assignments Done
99,4%
Successfully Done
In February 2019

# Answer to Question #55619 in C++ for harry

Question #55619
Write a program that reads a
whole number of up to nine digits and prints it in words.
For example, the input 13247 ought to produce "thirteen
thousand two hundred forty seven"

note: its for beginner dont use arrays or recursion
#include<limits>
#include <windows.h>
#include <string>
#include <iostream>
using namespace std;
string num_to_text[] = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
string tens_to_text[] = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
string power_to_text[] = { "", "Thousand", "Million", "Billion" };
string padIfNeeded (string ans)
{
if ( ans == "" )
{
return "";
}
return " " + ans;
}
string translateHundred (int hundred_chunk)
{
if ( hundred_chunk < 20 ) {
return num_to_text[ hundred_chunk ];
}
int tens = hundred_chunk / 10;
int ones = hundred_chunk % 10;
return tens_to_text[ tens ] + padIfNeeded( num_to_text[ ones ] );
}
string translateThousand (int thousand_chunk)
{
if ( thousand_chunk < 100 )
{
return translateHundred( thousand_chunk );
}
else
{
int hundreds = thousand_chunk / 100;
int hundred_chunk = thousand_chunk % 100;
return num_to_text[ hundreds ] + " Hundred" + padIfNeeded( translateHundred( hundred_chunk ) );
}
}
int main()
{
cout<<"\t\t\t Assalam-O-Alaikum!"<<endl;
while(1)
{
system("color 5B");
int n;
while((cout<<"\n\n Enter integer [till 9-didgits] : ")&&(!(cin>>n))){
cout<<"\n\t\tInvalid Input!"<<endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
cout<<"\n\t";

string number;
bool is_negative = false;
if ( n < 0 )
{
is_negative = true;
n *= -1;
}
int chunk_count = 0;
while ( n > 0 )
{
if ( n % 1000 != 0 ) {
number = translateThousand( n % 1000 ) + padIfNeeded( power_to_text[ chunk_count ] + padIfNeeded( number ) );
}
n /= 1000;
chunk_count++;
}
if ( number == "" )
{
number = "Zero";
}
if ( is_negative )
{
number = "Negative " + number;
}
cout << number << endl;
cout<<"\n\n\t ----------------------------------------------------------\n\n";
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!