Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

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.

  • This Question is Closed
  1. onurtuna
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    there are supposed to be up to 9 digits

  3. Muhammad.Ateeq.Azam
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    #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
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  5. Muhammad.Ateeq.Azam
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    just use switch statements instead of if statements :)

  6. JASdeep11
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ok

  7. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy