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!
Please code using C++ and only uss the header <iostream>. Any others will not be accepted. This
![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".](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F3f71a40a-4cff-4959-8946-a68dc332c184%2F5d47eee3-fb67-48fd-8a0d-ff5ba56a6ad6%2Fpmbxumm_processed.jpeg&w=3840&q=75)
![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.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F3f71a40a-4cff-4959-8946-a68dc332c184%2F5d47eee3-fb67-48fd-8a0d-ff5ba56a6ad6%2Fii1mnp_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
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.
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;
}
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)