Question

Write a program that uses a random number generator to generate a two digit positive integer...

Write a program that uses a random number generator to generate a two digit positive integer and allows the user to perform one or more of the following operations:

a. Double the number.

b. Reverse the digits of the number.

c. Raise the number to the power of 2, 3, or 4.

d. Sum the digits of the number.

e. If the number is a two digit number, then raise the first digit to the power of the second digit.

f. If the number is a three digit number and the last digit is less than or equal to 4, then raise the first two digits to the power of the last digit.

After performing an operation if the number is less than 10, add 10 to the number. Also, after each operation determine if the number is prime.

Each successive operation should be performed on the number generated by the last operation. Your program should not contain any global variables and each of these operations must be implemented by a separate function. Also, your program should be menu driven.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

// Function Prototypes
int doubleNumber(int x);
int reverseNumber(int x);
int raiseNumber(int x);
int sumNumber(int x);
int raiseFirstToSecondNumber(int x);
int raiseFirstTwoToLastNumber(int x);
int primeNumber(int x);
int lessThanTen (int x);
void printFunction(int x, int y);

int main()
{
        // Initialize variables
        int iRandom;
        int menuSelection;
        int primeYesNo;

        // Initialize random seed
        srand (time(NULL));
        
        // Generate random number
        iRandom = rand() % 100;
        /* TEST CASE iRandom = 28;  // PLACEHOLDER FOR RANDOM NUMBER = 28 *REMOVE**/
        cout  << "The two digit number is: " << iRandom << endl;

        do
        {

        // Menu Selection
        cout << "1: Enter 1 to double the number." << endl;
        cout << "2: Enter 2 to reverse the digits of the number." << endl; 
        cout << "3: Enter 3 to raise the number to the power of 2, 3, or 4." << endl; 
        cout << "4: Enter 4 to sum the digits of the number." << endl; 
        cout << "5: Enter 5 to raise the first digit to the power of the second digit." << endl; 
        cout << "6: Enter 6 to raise the first two digits to the power of the last digit." << endl; 
        cout << "9: Enter 9 to terminate the program." << endl;

        cin >> menuSelection;

        switch (menuSelection)
        {
        case 1:
                iRandom = doubleNumber(iRandom);
                primeYesNo = primeNumber(iRandom);
                printFunction (iRandom, primeYesNo);
                iRandom = lessThanTen (iRandom);
                break;
        case 2:
                iRandom = reverseNumber(iRandom);
                primeYesNo = primeNumber(iRandom);
                printFunction (iRandom, primeYesNo);
                iRandom = lessThanTen (iRandom);
                break;
        case 3:
                iRandom = raiseNumber(iRandom);
                primeYesNo = primeNumber(iRandom);
                printFunction (iRandom, primeYesNo);
                iRandom = lessThanTen (iRandom);
                break;
        case 4:
                iRandom = sumNumber(iRandom);
                primeYesNo = primeNumber(iRandom);
                printFunction (iRandom, primeYesNo);
                iRandom = lessThanTen (iRandom);
                break;
        case 5:
                iRandom = raiseFirstToSecondNumber(iRandom);
                primeYesNo = primeNumber(iRandom);
                printFunction (iRandom, primeYesNo);
                iRandom = lessThanTen (iRandom);
                break;
        case 6:
                iRandom = raiseFirstTwoToLastNumber(iRandom);
                primeYesNo = primeNumber(iRandom);
                printFunction (iRandom, primeYesNo);
                iRandom = lessThanTen (iRandom);
                break;
        default:
                break;

        system ("pause");

        }

        }
        while (menuSelection !=9);

        cout << endl;

        system ("pause");

        return 0;
}

int doubleNumber(int iRandom)
{
        iRandom = iRandom * 2;

        return iRandom;
}

int reverseNumber(int iRandom)
{
        // declare variables
        int ones = 0, tens = 0, huns = 0, thous = 0;
        
        // break into individual digits

        if (iRandom <10)
        {
                return iRandom;
        }

        if (iRandom < 100)
        {
                ones = iRandom%10;
                iRandom = iRandom/10;
                tens = iRandom%10;

                iRandom = ones*10 + tens*1;

                return iRandom;
        }

        if (iRandom <1000)
        {
                ones = iRandom%10;
                iRandom = iRandom/10;
                tens = iRandom%10;
                iRandom = iRandom/10;
                thous = iRandom%10;

                iRandom = ones*100 + tens*10 + thous*1;

                return iRandom;
        }
}

int raiseNumber(int iRandom)
{
        // declare variables
        double raiseTo;

        // additional input for raising iRandom to 2, 3, or 4
        cout << endl << "Enter power (2, 3, 4): ";
        cin >> raiseTo;
                
        iRandom = pow(iRandom, raiseTo);

        return iRandom;
}

int sumNumber(int iRandom)
{
        // declare variables

        int ones, tens, huns, thous;
        
        // break into individual digits
        ones = iRandom%10;
        iRandom = iRandom/10; // divide iRandom by 10 to get to tens
        tens = iRandom%10;
        iRandom = iRandom/10; // divide iRandom by 10 to get to hun
        huns = iRandom%10;
        iRandom = iRandom/10; // divide iRandom by 10 to get to thou
        thous = iRandom%10;

        iRandom = ones + tens + huns + thous;

        return iRandom;
}

int raiseFirstToSecondNumber(int iRandom)
{

        // declare variables
        double ones, tens;
        
        // break into individual digits
        ones = iRandom%10;
        iRandom = iRandom/10; // divide iRandom by 10 to get to tens
        tens = iRandom%10;

        iRandom = pow(tens, ones);

        return iRandom;
}

int raiseFirstTwoToLastNumber(int iRandom)
{
        if (iRandom <100 || iRandom > 999)
        {
                cout << endl;
                cout << "To perform this operation, it must be a three digit number." << endl;

                return iRandom;
        }

        double ones, tens, huns;
        
        // break into individual digits
        ones = iRandom%10;
        iRandom = iRandom/10; // divide iRandom by 10 to get to tens
        tens = iRandom%10;
        iRandom = iRandom/10; // divide iRandom by 10 to geth thous
        huns = iRandom%10;

        iRandom = pow(ones + tens*10, huns);

        return iRandom;
}

int primeNumber(int iRandom)
{
        int primeYesNo;
        int count = 0;

        for (int i = 2; i < iRandom; i++) // Loop to see if number is divisable
        {
                if (iRandom%i == 0)
                {
                        count++;
                        
                        break;
                }
        }

        if (count == 0) // see if check variable has changed (if no = prime number, if yes = not prime)
        {
                primeYesNo = 0;

                return primeYesNo;
        }
        else
        {
                primeYesNo = 1;

                return primeYesNo;
        }
}

void printFunction(int iRandom, int primeYesNo)
{
                if (primeYesNo == 0)
                {
                        cout << endl;
                        cout << iRandom << " is prime." << endl << endl;
                }
                else
                {
                        cout << endl;
                        cout << iRandom << " is not prime." << endl << endl;
                }
}

int lessThanTen (int iRandom)
{
        if (iRandom < 10)
        {
                cout << iRandom << " is less than 10, so adding 10 to it." << endl;
                iRandom = iRandom + 10;

                return iRandom;
        }

        return iRandom;
}
Add a comment
Know the answer?
Add Answer to:
Write a program that uses a random number generator to generate a two digit positive integer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program Instructions: Write a C++ program that uses a random number generator to generate a two...

    Program Instructions: Write a C++ program that uses a random number generator to generate a two digit positive integer and allows the user to perform one or more of the following operations: Double the number. Reverse the digits of the number. Raise the number to the power of 2, 3, or 4. Sum the digits of the number. If the number is a two digit number, then raise the first digit to the power of the second digit. If the...

  • Write a C program which calculates how many digits a number has and prints each digit...

    Write a C program which calculates how many digits a number has and prints each digit in reserve order with space in between(no space after the last digit). The number > 0 and has no more than 5 digits. (optional] It is better to write two functions. One is designed for printing number in reverse order and the other is for counting the number of digits. Given that the value of number (which is an int variable) is 12345, the...

  • Write in Java Write code which checks validity of a 3-digit positive integer entered by the...

    Write in Java Write code which checks validity of a 3-digit positive integer entered by the user. The number is considered valid (true) if the sum of the first two digits is less than the last. Otherwise it is invalid (false).

  • Digit to WordsWrite a C program that asks the user for a two-digit number, then prints...

    Digit to WordsWrite a C program that asks the user for a two-digit number, then prints the English wordfor the number. Your program should loop until the user wants to quit.Example:Enter a two-digit number: 45You entered the number forty-five.Hint: Break the number into two digits. Use one switch statement to print the word for the firstdigit (“twenty,” “thirty,” and so forth). Use a second switch statement to print the word for thesecond digit. Don’t forget that the numbers between 11...

  • Write a program using the random number generator to create random rainfall measurements and save these...

    Write a program using the random number generator to create random rainfall measurements and save these numbers in a text file. You will have more than 12 readings to cover more than 1 year of measurements. You may assume that we do not have more than 5 inches of rain in a month. Your program will work with a text file of any number of months (you do not know how many readings are contained in the input file). Using...

  • Write a python script that takes a six-digit integer from the user and separate the number...

    Write a python script that takes a six-digit integer from the user and separate the number into its individual digits. You program should then display each digit separated by a comma each. For example, if the user types in the number 654321, the script should display: 6,5,4,3,2,1 You can assume that the user enters the correct number of digits.

  • C++ exercise: Lottery Number Generator Design a program that generates a 7-digit lottery number. The program...

    C++ exercise: Lottery Number Generator Design a program that generates a 7-digit lottery number. The program should have an Integer array with 7 elements. Write a loop that steps through the array, randomly generating a number in the range of 0 through 9 for each element. (Use the random function that was discussed in Chapter 6.) Then write another loop that displays the contents of the array.

  • 3. Develop a random number generator that generates two-digit decimal numbers where individual digits appear with...

    3. Develop a random number generator that generates two-digit decimal numbers where individual digits appear with the following probabilities: digit 1 2 3 probability 0.1 0.2 0.3 0.4 For example, the number 43 (and 34) will be generated in 12% of cases, while l I will appear only in 1 % of cases.

  • Write a program that calls the random number generator two times to simulate rolling a pair...

    Write a program that calls the random number generator two times to simulate rolling a pair of dice. It should store the two numbers into variables. It will then determine if the dice roll is a winner (doubles OR add up to 7). Remember to add two includes and also to seed the random number generator to the clock. This will roll two dice and add them together. Doubles, or a total of 7 - YOU ARE A WINNER!!! First...

  • Objectives: Integer arithmetic, Functions, menus. Write a C++ program that displays the following menu of choices....

    Objectives: Integer arithmetic, Functions, menus. Write a C++ program that displays the following menu of choices. 1. Find the number of digits in an integer. 2. Find the nth digit in an integer. 3. Find the sum of all digits of an integer. 4. Is the integer a palindrome? 5. Quit Enter a choice: Page 1 of 4 For each of the choices (1, 3, 4), Read a positive integer number and call a function that processes the menu choice...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT