## JASdeep11 3 years ago 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". i need a step-by-step explanation not the code.

1. onurtuna

Just make the program read the numbers in an array then calculate the length of the number - how many numbers are there? pritn the number in words as how many numbers are

2. JASdeep11

there are supposed to be up to 9 digits

#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"; } }

4. JASdeep11

@Muhammad.Ateeq.Azam my teacher wants me to use the switch statement to write this program

just use switch statements instead of if statements :)

6. JASdeep11

ok

Find more explanations on OpenStudy