Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that uses a random number generator to generate a two digit posi

ID: 3539605 • Letter: W

Question

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

a.double the number

b.reverse the digit of the number

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

d.sum thee 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 powerof 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 operations should be performed on the number generated by the last peraion. Your program should not contain any global variables and each of these operations must be implemented by a seperate function. Also, your program should be MENU DRIVEN!!

code should be in c++ format that compile in visual studio 2010.

do not use array.


Explanation / Answer

please rate - thanks


you left some things unclear. I will make corrections - if I receive the points


#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 ";
else
     cout<<n<<" is not prime ";
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 ";
     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 ";
    cout<<"2.   reverse the digit of the number ";

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

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

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

cout<<"6.   If the number is a three digit number ";
cout<<"     and the last digit is less than or equal to 4      then raise the first two digits to the power of the last digit. ";
cout<<"7.   exit ";
cin>>choice;
}
return choice;
}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote