Question

In: Computer Science

Create a C++ program that will prompt the user to input an integer number and output...

Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only)

**Please only use #include <iostream> and switch and if-else statements only. Thank you.

Ex.
Enter a number: 68954
Sixty Eight Thousand Nine Hundred Fifty Four

Enter a number: 100000
One Hundred Thousand

Enter a number: -2
Number should be from 0-1000000 only

Solutions

Expert Solution

Code -

#include <iostream>
using namespace std;

//string stored from 1-19
string singleTwoDigitWords[] = { "", "One ", "Two ", "Three ", "Four ",
               "Five ", "Six ", "Seven ", "Eight ",
               "Nine ", "Ten ", "Eleven ", "Twelve ",
               "Thirteen ", "Fourteen ", "Fifteen ",
               "Sixteen ", "Seventeen ", "Eighteen ",
               "Nineteen "
           };
//storing ten multiple
string tenMultipleWords[] = { "", "", "Twenty ", "Thirty ", "Forty ",
               "Fifty ", "Sixty ", "Seventy ", "Eighty ",
               "Ninety "
           };

string convertNumberToWords(int num, string val)
{
   string str = "";
//check if the number is more than 19
   if (num > 19)
       str += tenMultipleWords[num / 10] + singleTwoDigitWords[num % 10];
   else
       str += singleTwoDigitWords[num];
//if number is not zero than concatenate string
   if (num)
       str += val;

   return str;
}
//function to covert the given integer number to words
string converNumberToWords(long num)
{
   string inWords;
//if number is divisible by 10000000 crore will added
   inWords += convertNumberToWords((num / 10000000), "Crore ");

//if number is divisible by 100000 lakh will added
   inWords += convertNumberToWords(((num / 100000) % 100), "Lakh ");

//if number is divisible by 1000 Thousand will added
   inWords += convertNumberToWords(((num / 1000) % 100), "Thousand ");

//if number is divisible by 100 Hundred will added

   inWords += convertNumberToWords(((num / 100) % 10), "Hundred ");

   if (num > 100 && num % 100)
       inWords += "and ";

   inWords += convertNumberToWords((num % 100), "");

   return inWords;
}

int main()
{
cout<<"Enter a number: "<<endl;
int num;
cin>>num;
if(num<0){
cout<<"Number should be from 0-1000000 only"<<endl;
}
else{
//method to convert the given number to words
   cout << converNumberToWords(num) << endl;
}

   return 0;
}

Screenhshots-


Related Solutions

Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
Create a C++ program that will prompt the user to input an positive integer number and...
Create a C++ program that will prompt the user to input an positive integer number and output the corresponding number to words. Check all possible invalid input data. (Please use only switch or if-else statements. Thank you.)
C++ code a program should prompt the user for the name of the output file. The...
C++ code a program should prompt the user for the name of the output file. The file should be opened for write operations. Values calculated by the program will be written to this file. The program must verify that the file opened correctly. If the file did not open, an error message should be printed and the user should be prompted again. This should continue until the user supplies a valid filename.
Write a C program that repeatedly prompts the user for input at a simple prompt (see...
Write a C program that repeatedly prompts the user for input at a simple prompt (see the sample output below for details). Your program should parse the input and provide output that describes the input specified. To best explain, here's some sample output: ibrahim@ibrahim-latech:~$ ./prog1 $ ls -a -l -h Line read: ls -a -l -h Token(s): ls -a -l -h 4 token(s) read $ ls -alh Line read: ls -alh Token(s): ls -a -l -h 2 token(s) read $...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
Write a program that accept an integer input from the user and display the least number...
Write a program that accept an integer input from the user and display the least number of combinations of 500s, 100s, 50s, 20s, 10s, 5s, and 1s. Test your solution using this samples] a. Input: 250 Output: 1x200s, 1x50s b. Input: 1127 Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s c. Input: 1127 Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s d. Input: 19 Output: 1x10s, 1x5s, 4x1s ​[Hints] o Use division to determine the number of occurrence of each element (i.e. 200, 100)...
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Important! Consider which control structures will work best for which aspect of the assignment. For example, which would be the best to use for a menu?...
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Your code must contain at least one of all of the following control types: nested for() loops a while() or a do-while() loop a switch() statement...
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
1. Write a program in C++ to find the factorial of a number. Prompt the user...
1. Write a program in C++ to find the factorial of a number. Prompt the user for a number and compute the factorial by using the following expression. Use for loops to write your solution code. Factorial of n = n! = 1×2×3×...×n; where n is the user input. Sample Output: Find the factorial of a number: ------------------------------------ Input a number to find the factorial: 5 The factorial of the given number is: 120 2. Code problem 1 using While...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT