1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <string>
using namespace std;
void AddTen(int num) //User-Defined Function for if num<10
{
if (num<10)
num =num + 10;
}
void isPrime(int num) //User-Defined Function for is prime or not
{
bool Prime = NULL;
for(int i = 1;i<sqrt(num);i++)
{
if(num%i ==0)
{
Prime = true;
break;
}
}
if (Prime == NULL)
{
cout<<"This is a prime number"<<endl;
}
else if(Prime = true)
{
cout<<"This is not a prime number"<<endl;
}
}
int main()
{
int sum = 0;
int num = 0;
string choice = " ";
while(num < 10) //Generate random number —Making sure it is greater than 10
{
srand(time(0));
num = rand()%100; // Generates random num <100
}
while(true)
{
cout<<"your number is "<<num<<endl;
cout<<"Choose an option: "<<endl;
cout<<"a. Double the number"<<endl;
cout<<"b. Reverse the digits of the number"<<endl; //Present User with Options
cout<<"c. Raise the number to the power of 2, 3, 4"<<endl;
cout<<"d. Sum the digits of the number"<<endl;
cout<<"e. less than 2 digits or greater than 3"<<endl;
cout<<"f. exit program"<<endl;
cin>>choice;
if(choice == "a")
{
num = num*2; // double the number
AddTen(num);
isPrime(num); //calling my functions
}
else if(choice == "b")
{
string result = "";
string StrRev = to_string(num); //convert num to string
for (int j = StrRev.length(); j >= 0; j–)
{
string a = StrRev.substr(j,1); //Use subsets
result = result + a;
}
num = stoi(result);
AddTen(num);
isPrime(num);
}
else if(choice == "c")
{
char powerChoice = ' ';
cout<<"Choose to raise to the power of:"<<endl;
cout<<"a. 2"<<endl;
cout<<"b. 3"<<endl;
cout<<"c. 4"<<endl;
cin>>powerChoice;
if(powerChoice == 'a') //User chooses what power
{
num = pow(num, 2);
}
else if(powerChoice == 'b' )
{
num = pow(num, 3);
}
else if(powerChoice == 'c')
{
num = pow(num, 4);
}
AddTen(num);
isPrime(num);
}
else if(choice == 'd')
{
string str = to_string(num); //convert to string
int holder = 0;
for(int i = 0; i < str.length();i++)
{
string y = str.substr(i,1); //User subsets for individual digits
holder = stoi(y); //Convert subset to numerical
sum = sum + holder; //add sum
num = sum;
}
AddTen(num);
isPrime(num);
}
else if(choice == 'e')
{
string length = to_string(num);
if(length.length() == 2) //Figures out whether 2 or 3 digits
{
string num1 = length.substr(0,1);
string num2 = length.substr(1,1);
int numb1 = stoi(num1);
int numb2 = stoi(num2);
num = pow(numb1, numb2); //uses individual digits to set (num1)^(num2)
}
string substr = length.substr(length.length() – 1,1);
if(length.length() == 3 && substr.length() <= 4)
{
string num1 = length.substr(0,2);
string num2 = length.substr(2,1);
int numb1 = stoi(num1);
int numb2 = stoi(num2);
num = pow(numb1, numb2); //Finds 2dig and 1dig numbers
}
AddTen(num);
isPrime(num);
}
else
{
break; //User chooses to exit
}
cout<<"Your new number is "<<num<<endl;
}
return 0;
}
| |