1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
mars1129 [50]
4 years ago
12

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: 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 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.
Computers and Technology
1 answer:
Anna007 [38]4 years ago
4 0

Answer:

#include <iostream>

#include <ctime>

#include <cstdlib>

using namespace std;

int menu();

int doubleIt(int);

int getPower(int,int);

int reverse(int);

int power(int);

int sum(int);

int twoDigit(int);

int threeDigit(int);

bool prime(int);

int main()

{int n;

srand(time(0));

n=rand()%90+10;

do

{

cout<<"The number is: "<<n<<endl<<endl;

switch(menu())

  {case 1: n=doubleIt(n);

           break;

  case 2: n=reverse(n);

          break;

  case 3: n=power(n);

          break;

  case 4: n=sum(n);

          break;

  case 5: n=twoDigit(n);

          break;

  case 6: n=threeDigit(n);

          break;

  case 7: return 0;

  }

if(prime(n))

    cout<<n<<" is prime\n";

else

    cout<<n<<" is not prime\n\n";

if(n<10)

    n+=10;

   

}while(true);

}

int doubleIt(int n)

{return n*2;

}

int power(int n)

{int p;

cout<<"Enter 2, 3 or 4th power: ";

cin>>p;

while(p<2||p>4)

   {cout<<"invalid entry\n";

    cout<<"Enter 2, 3 or 4th power: ";

    cin>>p;

    }

return getPower(n,p);

}

int reverse(int n)

{int newnum=0;

while(n>0)

     {newnum=newnum*10+n%10;

      n=n/10;

     }

return newnum;  

}

int sum(int n)

{int sum=0;

while(n>0)

     {sum=sum+n%10;

      n=n/10;

     }

return sum;

}

int twoDigit(int n)

{int d1,d2;

   if(n<100)

      {d1=n%10;

       d2=n/10;

       return getPower(d1,d2);

       }

return n;

}

int threeDigit(int n)

{int d1,n2;

if(n>99)

  {d1=n%10;

   if(d1<=4)

       {n2=n/10;

        return getPower(n2,d1);

        }

}

return n;

}

bool prime(int n)

{int i;

for(i=2;i<n-1;i++)

  if(n%i==0)              

     return false;

return true;                

}

int getPower(int x,int y)

{int ans,i;

if(y==0)

     return 1;

  else

     {ans=x;

      for(i=2;i<=y;i++)

          ans=ans*x;

      return ans;

      }

      }

int menu()

{int choice=8;

while(choice<1||choice>7)

  {cout<<"1.   double the number\n";

   cout<<"2.   reverse the digit of the number\n";

cout<<"3.   raise the number to the power of 2,3, or 4\n";

cout<<"4.   sum thee digits of the number.\n";

cout<<"5.   if the number is a two digit number\n     then raise the first digit to the power of the second digit\n";

cout<<"6.   If the number is a three digit number\n";

cout<<"     and the last digit is less than or equal to 4\n     then raise the first two digits to the power of the last digit.\n";

cout<<"7.   exit\n";

cin>>choice;

}

return choice;

}

You might be interested in
In Microsoft word you can access the ______ command from "mini toolbar"
Mashcka [7]
Redo command, i think.
8 0
3 years ago
Read 2 more answers
What guidelines should you follow when adding graphics to your presentations?
Elena L [17]
B, C, and D are the correct answers.
3 0
4 years ago
Read 2 more answers
Concept of CPU scheduler?​
vekshin1

Answer:

CPU scheduling is a process which allows one process to use the CPU while the execution of another process is on hold(in waiting state) due to unavailability of any resource like I/O etc, thereby making full use of CPU. ... The selection process is carried out by the short-term scheduler (or CPU scheduler).

6 0
3 years ago
Which of the following would increase the Demand for jam?
gladu [14]
The answer would be B. An increase in the price of a complement.
8 0
3 years ago
Is the conversation recorded when the party answers or when they dial the number?
Len [333]
I think it’s when the party answers
3 0
3 years ago
Other questions:
  • Today car-buying customers can enter negotiation armed with accurate facts and figures about a car. Describe how car buyers felt
    9·1 answer
  • Windows server 2012 r2 includes hyper-v in which edition(s)?
    12·1 answer
  • Which of the following is a group of email names and addresses used for mass distribution of a message?
    5·1 answer
  • Henry went to an IT software company for an interview he was offered a position in lower level management which positions could
    11·1 answer
  • If all the data in a database is not physically located in one place, it would be a(n _______ database.
    5·1 answer
  • The most important task in developing a new computer system/app is to: a) choose the most current technologies such as operating
    5·1 answer
  • A type of SDLC in which each phase has results that flow into the next phase is called the __________ model.
    8·1 answer
  • What does mean in computer science
    10·1 answer
  • In a paragraph discuss 5 steps of saving a document in a storage device.<br>​
    5·1 answer
  • You are the security analyst for your organization and have discovered evidence that someone is attempting to brute-force the ro
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!