Question

In: Computer Science

Write a program that prompts a user for an integer from 1 to 99 and prints...

Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words.  
The program will loop in case the user wants to input an additional number.
If the user enters -99, the program will exit.

Example:
Input: 89
Output: Eighty nine

Input: 45
Output: Fourty five

Input: -99
Output: Have a nice day.
<program exits>

c++ project. need help.

Solutions

Expert Solution

#include <iostream>
#include <limits>

using namespace std;

int digitsInNumber; // global variable to store 
bool validNumber(int); // function to check whether entered number is single digit or two digit
void isSingleDigit(int);//if number is single digit
void lessThan19(int); // if number is two digit and less than 19
void greaterThan19(int);// if number is two digit and greater than 10
int main()
{
    int n=0; // to store user entered number
    bool reEnter=true;// to check whether entered number is valid i.e 1-99 or -99
    bool whetherInteger=true;
    // this loop will take input from user and check whether number is between 1-99 or -99
    do{
        cin>>n;
        if(!cin) //if value is not integer
        {
          cin.clear();
          cin.ignore(numeric_limits<streamsize>::max(), '\n');
          whetherInteger=false;
          
        }
        else //if value is integer
        {
            if((n>0 &&n<100)|| n==-99)
            {
              reEnter=false;
            }
        }
    }while(reEnter==1);

    // loop to calculate number of digits in the user entered number
    int temp=n;
    int count=0;
    while (temp != 0) { 
       temp = temp / 10; 
        ++count;
        digitsInNumber=count;
    }
    
    // for our output there are 4 cases
    // case1: when number is -99
    // case2: when number is between 1-9
    // case3: when number is between 10-19
    // case4: when number is between 20-99
    // if number is -99 output will be "have a nice day"
    if(n==-99) // case 1
    {
      cout<<"Have a nice day";
    } 
    else
    {
      if(digitsInNumber==1) // case 2
      {
        isSingleDigit(n);
      }
      if(digitsInNumber==2)
      {
        if(n<20) //case 3
        {
          lessThan19(n);
        }
        else // case 4
        {
          greaterThan19(n); 
        }
      }
  }
    return 0;
}
// This method will run single digit number 
// it will take number as argument and will print specific word
void isSingleDigit(int n)
  {
    switch(n)
    {
      case 1:cout<<"one ";break;
      case 2:cout<<"two ";break;
      case 3:cout<<"three ";break;
      case 4:cout<<"four ";break;
      case 5:cout<<"five ";break;
      case 6:cout<<"six ";break;
      case 7:cout<<"seven ";break;
      case 8:cout<<"eight ";break;
      case 9:cout<<"nine ";break;
    }
  }

// This method will run two digit number from 10-19
// it will take number as argument and will print specific word
  void lessThan19(int n)
  {
    switch(n)
    {
      case 10:cout<<"ten";break;
      case 11:cout<<"eleven ";break;
      case 12:cout<<"twelve ";break;
      case 13:cout<<"thirteen ";break;
      case 14:cout<<"fourteen ";break;
      case 15:cout<<"fifteen ";break;
      case 16:cout<<"sixteen ";break;
      case 17:cout<<"seventeen ";break;
      case 18:cout<<"eighteen ";break;
      case 19:cout<<"nineteen ";break;
    
    }
  }

  // This method will take number from 20-99
  // we have similarities in these numbers
  // Their 10's place digit are same for 10 numbers
  void greaterThan19(int n)
  {
    int digit1=n%10; // unit place of number
    n=n/10;
    int digit0=n%10; //  10's place of number
    // for digit 0 we can use specific words and for digit we call the method same as thaf of single digit number
    switch(digit0) 
    {
      case 2:cout<<"twenty ";break;
      case 3:cout<<"thirty ";break;
      case 4:cout<<"fourty ";break;
      case 5:cout<<"fifty ";break;
      case 6:cout<<"sixty ";break;
      case 7:cout<<"seventy ";break;
      case 8:cout<<"eighty ";break;
      case 9:cout<<"ninety ";break;
    }
    if(digit1>0)
    {
      isSingleDigit(digit1);
    }
  }

Related Solutions

Write a program that prompts a user for an integer from 1 to 99 and prints...
Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words. The program will loop in case the user wants to input an additional number. If the user enters -99, the program will exit. Example: Input: 89 Output: Eighty nine Input: 45 Output: Fourty five Input: -99 Output: Have a nice day. <program exits> For this project, you are to: 1) You should validate any data coming from the...
Write a program which prompts the user for a positive integer, and then prints out the...
Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response. Do not import anything other than the Scanner. One way you might go about this using nested loops: Start a "factor" variable at 2 In a loop: repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor increment the factor This plan is by no stretch...
in C++ programing language Write a program that prompts the user for an integer, then prints...
in C++ programing language Write a program that prompts the user for an integer, then prints all of the numbers from one to that integer, separated by spaces. Use a loop to print the numbers. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Drop to a new line after printing each 20 numbers. If the user typed...
Write a program that prompts the user to enter an integer from 1 to 15 and...
Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid, as shown in the following sample run: here............THE PYRAMID HAS TO BE THIS SHAPE AND IT IS DONE IN JAVA PLEASE 7 6 5 4 3 2 1 2 3 4 5 6 7 6 5 4 3 2 1 2 3 4 5 6 5 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4...
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
Problem 1 Write a program that prompts the user to enter an integer It then tells...
Problem 1 Write a program that prompts the user to enter an integer It then tells the user if the integers is a multiple of 2, 3, 5, 7 or none of the above. Program language is C Ex. Enter an integer 12 You entered 12 The number you entered is a multiple of 2 ----------------------------------------------- Enter an integer 11 You entered 11 The number you entered is not a multiple of 2, 3, 5, or 7
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
Write a program which: Prompts the user for a positive integer >= 0 Validates the user...
Write a program which: Prompts the user for a positive integer >= 0 Validates the user input to ensure it is a positive integer >= 0 Allocate (dynamically) an array big enough for the data. Load the array with random numbers ranging in value from1 to 100 Display the elements of the array (unsorted) Display the elements of the array (sorted) Display the average Display the median Display the mode, if none, display appropriate message RESTRICTIONS No global variables No...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT