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
Given two integers as user inputs that represent the number of drinks to buy and the number of bottles to restock, create a Vend
Zanzabum

Answer:

import java.util.Scanner;

class Main {

 public static void main (String args[])

 {

   Scanner input = new Scanner(System.in);

   VendingMachine machine = new VendingMachine(20);

   System.out.print("Enter nr to purchase: ");

   int nrPurchased = input.nextInt();

   System.out.print("Enter nr to re-stock: ");

   int nrRestock = input.nextInt();

   machine.Transaction(nrPurchased, nrRestock);

   input.close();

 }

}

class VendingMachine {

 private int inventory;

 public VendingMachine(int initialInventory) {

   inventory = initialInventory;

 }

 public void Transaction(int nrToBuy, int nrToRestock) {

   inventory += nrToRestock - nrToBuy;

   System.out.printf("Inventory: %d bottles.\n", inventory);

 }

}

7 0
3 years ago
Why does Linux make use of tasklets (i.e., software interrupts) instead of executing all interrupt-related activity in the (hard
Eddi Din [679]

Answer:

Although some devices can be controlled using nothing but their I/O regions, most real devices are a bit more complicated than that. Devices have to deal with the external world, which often includes things such as spinning disks, moving tape, wires to distant places, and so on. Much has to be done in a time frame that is different from, and far slower than, that of the processor. Since it is almost always undesirable to have the processor wait on external events, there must be a way for a device to let the processor know when something has happened.

That way, of course, is interrupts. An interrupt is simply a signal that the hardware can send when it wants the processor's attention. Linux handles interrupts in much the same way that it handles signals in user space. For the most part, a driver need only register a handler for its device's interrupts, and handle them properly when they arrive. Of course, underneath that simple picture there is some complexity; in particular, interrupt handlers are somewhat limited in the actions they can perform as a result of how they are run.

5 0
3 years ago
How does FTP work? explain in brief.
nikitadnepr [17]

The File Transfer Protocol (FTP) is used to transfer files between two computers over a network and Internet.

5 0
3 years ago
When writing professional emails, it is okay to use emoticons and
nekit [7.7K]

Answer:

False

Explanation:

8 0
3 years ago
Read 2 more answers
Differences between analog computer and hybrid computer​
aleksandrvk [35]

Answer:

Analog computers only work with continuous numerical data in analog quantities, digital computers can process both non-numerical and numerical data and a hybrid computer is a combination of both analog and digital. A hybrid computer has the accuracy of a digital computer paired with speed of an analog one.

5 0
3 years ago
Read 2 more answers
Other questions:
  • Select the correct answer.
    8·2 answers
  • Josh needs to write a research report for his Civics class. Which file type will allow him to save his file?
    12·2 answers
  • One critique of determining the effectiveness of the psychodynamic perspective is that its theories are too vague to test. t/f
    14·2 answers
  • What is the simplest way to permanently get rid of an unwanted file? A. Go to the Start menu and then delete the file. B. Erase
    5·1 answer
  • The number of protons plus the number of nuetrons equal the <br> A.atomic weight<br> B.atomic number
    10·1 answer
  • Why should you follow up with your interview after a job interview
    15·2 answers
  • Which of the following statements is NOT true regarding the Security Configuration and Analysis (SCA) tool?
    12·1 answer
  • Information systems include all of these
    5·1 answer
  • PLEASE HELP! Please dont answer if your going to guess
    14·2 answers
  • Memory cards from your cameras (SD cards) and flash drives are examples of __________.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!