Respuesta :
Answer:
// here is code in C++.
#include<bits/stdc++.h>
using namespace std;
// function to double the number
int double_num(int n)
{
return n*2;
}
// function to reverse the digits
int reverse_num(int n)
{
int rev_num=0;
while(n>0)
{
rev_num=rev_num*10+n%10;
n=n/10;
}
return rev_num;
}
// function to calculte sum of digits
int sum_digits(int n)
{
int sum_d=0;
while(n>0)
{
sum_d=sum_d+n%10;
n=n/10;
}
return sum_d;
}
// function to calculte power
int get_Pow(int x,int y)
{
int ans_pow,i;
if(y==0)
return 1;
else
{ans_pow=x;
for(i=2;i<=y;i++)
ans_pow=ans_pow*x;
return ans_pow;
}
}
//defintion of the twoDigit function
int twoDigit(int n)
{
int d1,d2;
if(n<100)
{d1=n%10;
d2=n/10;
return get_Pow(d1,d2);
}
return n;
}
//defintion of the threeDigit function
int threeDigit(int n)
{
int d1,n2;
if(n>99)
{d1=n%10;
if(d1<=4)
{n2=n/10;
return get_Pow(n2,d1);
}
}
return n;
}
//defintion of the menu function
int menu()
{
int choice=8;
//while user input is not between 1 and 7 display menu
while(choice<1||choice>7)
{
//display the menu
cout<<"1. double the number\n";
cout<<"2. reverse the digit of the number\n";
cout<<"3. raise the number to the power of 3\n";
cout<<"4. sum of digits of the number.\n";
cout<<"5. if random number is two digit number\n raise the first to the power of the second digit\n";
cout<<"6. If random number is a three digit number\n";
cout<<" and the last digit is less than or equal to 4\n then raise first two digits to the power of the last digit.\n";
cout<<"7. exit\n";
cin>>choice;
}
return choice;
}
// driver function
int main()
{
int n;
// generate two digit random number
n=rand()%90+10;
int result;
do
{
cout<<"The number is: "<<n<<endl<<endl;
// get user option
switch(menu())
{
case 1: result=double_num(n);
cout<<"duble of number is: "<<result<<endl;
break;
case 2: result=reverse_num(n);
cout<<"reverse of number is: "<<result<<endl;
break;
case 3: result=get_Pow(n, 4);
cout<<"number to power 4 is: "<<result<<endl;
break;
case 4: result=sum_digits(n);
cout<<"sum of digits is: "<<result<<endl;
break;
case 5: result=twoDigit(n);
cout<<"first digits power to second : "<<result<<endl;
break;
case 6: result=threeDigit(n);
cout<<"Ans: "<<result<<endl;
break;
case 7: return 0;
}
}while(true);
}
Explanation:
Generate a two digit random number. then ask from user to give his option. On the basis of user's choice it will call the function.Like if user option is 1 then it will calculate the twice of number, If option is 2 function will revers the number. Similarly it will call the other functions on the basis of user option.If the option is 7 then program exit.
Output:
The number is: 83
1. double the number
2. reverse the digit of the number
3. raise the number to the power of 3
4. sum of digits of the number.
5. if random number is two digit number
raise the first to the power of the second digit
6. If random number is a three digit number
and the last digit is less than or equal to 4
then raise first two digits to the power of the last digit.
7. exit
1
double of number is: 166
The number is: 83
1. double the number
2. reverse the digit of the number
3. raise the number to the power of 3
4. sum of digits of the number.
5. if random number is two digit number
raise the first to the power of the second digit
6. If random number is a three digit number
and the last digit is less than or equal to 4
then raise first two digits to the power of the last digit.
7. exit
2
reverse of number is: 38
The number is: 83
1. double the number
2. reverse the digit of the number
3. raise the number to the power of 3
4. sum of digits of the number.
5. if random number is two digit number
raise the first to the power of the second digit
6. If random number is a three digit number
and the last digit is less than or equal to 4
then raise first two digits to the power of the last digit.
7. exit
7