Question

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:

  1. Double the number.
  2. Reverse the digits of the number.
  3. Raise the number to the power of 2, 3, or 4.
  4. Sum the digits of the number.
  5. If the number is a two digit number, then raise the first digit to the power of the second digit.
  6. 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. (#include <string>)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<math.h>
using namespace std;

int randomNumber(int min , int max){
   return min + rand()%(max-min+1);
}

int doubleNumber(int num){
   return num*2;
}
int reverseDigit(int num){
   int reverse=0;
   while(num >0){
       reverse*=10;
       reverse+=num%10;
       num/=10;
   }
   return reverse;
}

int raise(int num , int power){
   int value = 1;
   for(int i=1;i<=power;i++){
       value*=num;
   }
   return value;
}

int sumDigits(int num){
   int sum = 0;
   while(num >0){
       sum+=num%10;
       num/=10;
   }
   return sum;
}

int raisePower(int num){
   if(num<100 && num>9){
       return raise(num/10 , num%10);
   }
   if(num>99 && num<1000){
       if(num%10 <=4){
           return raise(num/10 , num%10);
       }
   }
}

bool isPrime(int num){
   if(num <= 1)return false;
   for(int i=2;i<=sqrt(num);i++){
       if(num%i == 0)return false;
   }
   return true;
}

void menu(){
   cout<<"\n1.Double the number\n2.Reverse Digits\n3.Raise the number to the power of 2, 3, or 4\n";
   cout<<"4.Sum the digits of the number.\n5.number operation\n6.Quit\n";
}

int main(){
   srand(time(NULL));
   int number,choice,power;
   cout<<"Generating 2 digits random number \n";
   number = randomNumber(10,99);
  
   do{
       menu();
       cout<<"Enter choice : ";
       cin>>choice;
      
       switch(choice){
           case 1:{
               number = doubleNumber(number);
               break;
           }
           case 2:{
               number = reverseDigit(number);
               break;
           }
           case 3:{
               cout<<"Enter value of power(either 2 ,3 or 4): ";
               cin>>power;
               number = raise(number , power);
               break;
           }
           case 4:{
               number = sumDigits(number);
               break;
           }
           case 5:{
               number = raisePower(number);
               break;
           }
           case 6:{
               cout<<"Thank You!\n";
               break;
           }
           default:{
               cout<<"Invalid choice\n";
               break;
           }
       }
      
       if(isPrime(number))
           cout<<number<<" is prime\n";
      
       else cout<<number<<" is not prime\n";
      
       if(number<10){
           cout<<"Number is "<<number<<" less than 10\n";
           number+=10;
       }
       cout<<"\nNumber is "<<number<<"\n";
      
   }while(choice!=6);

return 0;
}

//sample output

Add a comment
Know the answer?
Add Answer to:
Program Instructions: Write a C++ program that uses a random number generator to generate a two...
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
  • 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....

  • 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 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...

  • PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which...

    PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which will simulate a calculator. Your calculator needs to support the five basic operations (addition, subtraction, multiplication, division and modulus) plus primality testing (natural number is prime if it has no non-trivial divisors). : Rewrite your lab 3 calculator program using functions. Each mathematical operation in the menu should be represented by a separate function. In addition to all operations your calculator had to support...

  • a c++ program: For this project you will write a program to add very large (unsigned)...

    a c++ program: For this project you will write a program to add very large (unsigned) integers. The two summands should be stored arrays, one array for each summand. The summands should be stored with one digit per location in the two arrays. Your program should, starting at the least significant digits compute the sum by adding the two integers digit by digit. Do not forget to handle carries. Your program should be able to handle summands with up to...

  • 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...

  • 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.

  • Write a C program that uses the random number generator rand( ) to create an array...

    Write a C program that uses the random number generator rand( ) to create an array with 20 numbers with value in the range from 1 to 100. The program calculates and displays the difference between the largest array element and the second largest array element in the array.

  • 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...

  • PROGRAMMING IN C. 1) The first program will compute compounded interest. Suppose you invest $1000.00 at...

    PROGRAMMING IN C. 1) The first program will compute compounded interest. Suppose you invest $1000.00 at an interest rate of 2% per year. If the interest is computed at the end of each day, it is added to your account (i.e., the interest is compounded daily). Print what the account value will be at the end of each year for 20 years. Use data type double for money. For 365 days per year (ignore leap year), the daily interest rate...

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
ADVERTISEMENT