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.
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 power of the last digit.
#include <iostream>
#include <cstdlib>
#include <ctime>
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;
// the first generated random number does not differ much from the starting one in Visual Studio
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;
}
Comments
Leave a comment