Answer to Question #183020 in C++ for Vincenzo Santoro

Question #183020

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:

  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.

Grading

You will not be graded on this lab.

Configuring sandbox environment...



1
Expert's answer
2021-04-30T23:13:43-0400
#include <bits/stdc++.h>






int doubleNumber(int n) 
{
    return 2 * n;
}
int reverseDigital(int n)
{
    return 10 * (n % 10) + n / 10;
}
int powerNumber(int n) 
{
    int m;
    std::cout << "Enter power [2,3,4] : ";
    std::cin >> m;
    return pow(n, m);
}
int sumDigital(int n) 
{
    return n / 10 + n % 10;
}
int powerDigital(int n)
{
    if (n < 10 || n>99)
    {
        std::cout << "Incorrect entry for this function" << std::endl;
        return -1;
    }
    else
    {
        return pow(n / 10, n % 10);
    }
}
    int powerThree(int n) 
    {
        if (n < 100 || n>999 || n%10>4) 
        {
            std::cout << "Incorrect entry for this function " << std::endl;
            return -1;
        }
        else 
        {
            return pow(n % 100, n % 10);
        }
}




int main()
{
   int n;
   char choise [7];
   srand(time(0));
   n=rand() % 90 + 10;
   
   n = rand() % 90 + 10;
   std::cout <<"Generate number: " <<n<<std::endl;
   std::cout<<"List of possible operations on a number: " << std::endl;
   std::cout <<"Letters [a,b,c,d,e,f] are the operation code" << std::endl;
   std::cout << "a. Double the number." << std::endl;
   std::cout << "b. Reverse the digits of the number." << std::endl;
   std::cout << "c. Raise the number to the power of 2, 3, or 4." << std::endl;
   std::cout << "d. Sum the digits of the number." << std::endl;
   std::cout << "e. If the number is a two digit number, then raise the first digit to the power of the second digit." << std::endl;
   std::cout << "f.   If the number is a three digit number and the last digit is less than or equal to 4,";
   std::cout<<"       then raise the first two digits to the power of the last digit." << std::endl;
   std::cout << " Enter opcodes on one line without separation: ";
   std::cin.getline(choise, 7);
   for (int i = 0; i < 6; i++) 
   {
       if (choise[i] == 'a') 
       {
           std::cout << "Double the number "<<doubleNumber(n)<<std::endl;
       }
       if (choise[i] == 'b') 
       {
           std::cout << "Reverse the digits of the number  " << reverseDigital(n) << std::endl;
       }
       if (choise[i] == 'c')
       {
           std::cout << "Number to the power "<<powerNumber(n) << std::endl;;
       }
       if (choise[i] == 'd')
       {
           std::cout << "Sum the digits of the number " << sumDigital(n) << std::endl;;
       }
       if (choise[i] == 'e') 
       {
           std::cout << "First digit to the power of the second digit " << powerDigital(n) << std::endl;;
       }
       if (choise[i] == 'f') 
       {
           std::cout << "Three digit number power user function"<<powerThree(n) << std::endl;;
       }
   }
   return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS