Please code using C++ and only uss the header . Any others will not be accepted. This program is supposed to be written using switch and if statements by I can't figure it out. I wrote it using strings so I need the help. Thank you!

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Please code using C++ and only uss the header <iostream>. Any others will not be accepted. This program is supposed to be written using switch and if statements by I can't figure it out. I wrote it using strings so I need the help. Thank you!

10: numberWords.cpp) 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".
Transcribed Image Text:10: numberWords.cpp) 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".
numberWords.cpp
#include <iostream>
using namespace std;
int main()
{
il break the number into three three-digit numbers
Ilwrite the first three-digit number
INrite "million"
Iwrite the second three-digit number
INrite "thousand"
Ilwrite the third three-digit number
INrite an endline
return 0;
To write this program then: First break the number into three three-digit numbers. Then
write the first three-digit number. Write "million". Write the second three-digit number.
Write "thousand". Finally, write the last three-digit number.
Transcribed Image Text:numberWords.cpp #include <iostream> using namespace std; int main() { il break the number into three three-digit numbers Ilwrite the first three-digit number INrite "million" Iwrite the second three-digit number INrite "thousand" Ilwrite the third three-digit number INrite an endline return 0; To write this program then: First break the number into three three-digit numbers. Then write the first three-digit number. Write "million". Write the second three-digit number. Write "thousand". Finally, write the last three-digit number.
Expert Solution
Step 1

PROGRAM STRUCTURE:

  • Include the required header files in the program.
  • Declare the string type variables to store the words of ones place and tens place.
  • Start the definition of the function when the number is 1-digit or 2-digit.
    • When the number is more than 19 then use the division operator to perform the required task.
    • Concatenate the string when the number is non zero.
  • Start the definition of the function to display the word form of the number.
    • Handle the word form according to the position of the digit in the complete number.
  • Start the definition of the main driver function.
    • Declare the required variables.
    • Take the value of the number from the user.
    • Call the function to display the number in its corresponding words form.
    • Display the word form of the number.
Step 2

PROGRAM CODE:

 

#include <iostream>                              // header file for input output stream
using namespace std; 

string one[] = { "", "one ", "two ", "three ", "four ",             // string type variable to store the words for ones place
                 "five ", "six ", "seven ", "eight ", 
                 "nine ", "ten ", "eleven ", "twelve ", 
                 "thirteen ", "fourteen ", "fifteen ", 
                 "sixteen ", "seventeen ", "eighteen ", 
                 "nineteen " }; 
  
string ten[] = { "", "", "twenty ", "thirty ", "forty ",              // string type variable to store the words for the tens place
                 "fifty ", "sixty ", "seventy ", "eighty ", 
                 "ninety " }; 
  
string numToWords(int n, string s)                  // function when the number is of 1-digit or 2-digit

    string str = ""; 
    if (n > 19)                                            // condition when the number is more than 19 then divide it
        str += ten[n / 10] + one[n % 10]; 
    else
        str += one[n]; 

    if (n)                                                   // condition when the n is non zero
        str += s; 
  
    return str; 

  
string convertToWords(long n)               // function definition to display the number in words

    string out;                       // variable to store the words representation of the number
    out += numToWords((n / 10000000), "crore "); 
    out += numToWords(((n / 100000) % 100), "lakh ");  // statement to handle one million place number
    out += numToWords(((n / 1000) % 100), "thousand "); 
    out += numToWords(((n / 100) % 10), "hundred ");    // statement to handle the hundred place number
    out += numToWords((n % 100), ""); 
  
    return out; 

int main()                                   // definition of the driver function 

    long n;                                   // declare the required variables
    cout<<"Enter the number: ";
    cin>>n;                                  // take the number from the user
    cout << convertToWords(n) << endl;       // display the word form of the number
  
    return 0; 

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Algebraic Expressions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education