In: Computer Science
C++ Programa that:
// 1) Ask the user to enter two numbers greater than zero. Store the
// values into two variables named 'n' and 'm'.
// Additionally, implement a while-loop to keep asking the user to enter
// the numbers again if any of the two numbers are equal or smaller than zero.
// 2) If the two numbers (n, m) are equal, display the message "The
// numbers are the same". If the two numbers are different, display
// the message "The numbers are not the same".
// 3) Calculate and display the number n elevated into the power of m: n^m
// Store the result into a variable named 'X'.
// HINT: Use the pow() function.
// EXAMPLE: If n = 3 and m = 2 then X = 3^2 = 3 * 3 = 9
// EXAMPLE: If n = 2 and m = 3 then X = 2^3 = 2 * 2 * 2 = 8
// EXAMPLE: If n = 5 and m = 1 then X = 5^1 = 5
// EXAMPLE: If n = 1 and m = 3 then X = 1^3 = 1 * 1 * 1 = 1
// 4) The result from (c) is already stored into the variable 'X'.
// Calculate and display the factorial for the value in 'X': X!
// Store the result into a variable named 'factorial'.
// HINT: Use a for-loop similar to the for-loop used to calculate the
// mean of a sequence of numbers in the file 5.3-For.cpp. Instead
// of adding numbers, now we want to multiply numbers.
// EXAMPLE: If X = 9, then factorial = 9! = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 = 362880
// EXAMPLE: If X = 8, then factorial = 8! = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 = 40320
// EXAMPLE: If X = 5, then factorial = 5! = 1 * 2 * 3 * 4 * 5 = 120
// EXAMPLE: If X = 1, then factorial = 1! = 1
// NOTE: The calculation of a factorial can result in a really a big number,
// and the compiler may give an incorrect result. Use the numbers
// provided in the examples to test your code.
// 5) Add code to ask the user if he wants to quit the program. If
// the user enter 'yes' the program exits, if the user enter 'no' or any
// other string, the program will execute again.
#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
int main(){
int n,m;
string s;
do{
// 1) Ask the user to enter two
numbers greater than zero. Store the
// values into two variables named
'n' and 'm'.
// Additionally, implement a
while-loop to keep asking the user to enter
// the numbers again if any of the
two numbers are equal or smaller than zero.
while(1){
cout<<"enter two numbers greater than zero: ";
cin >> n
>> m;
if(n==m ){
cout <<"The numbers are the
same"<<endl;;
}else if(n <=
0 || m <= 0){
cout <<"smaller than
zero"<<endl;
}else{
cout <<"The number are not
same."<<endl;
break;
}
}
//Calculate and display the number
n elevated into the power of m: n^m
//Store the result into a variable
named 'X'.
int X = pow(n,m);
cout<<"Power of n^m:
"<<X<<endl;
// 4) The result from (c) is
already stored into the variable 'X'.
//Calculate and display the
factorial for the value in 'X': X!
int factorial = 1;
for(int i = 1; i <= X;
i++){
factorial *=
i;
}
cout<<"Factorial of
"<<X<<"! is: "<<factorial<<endl;
// 5) Add code to ask the user if
he wants to quit the program. If
// the user enter 'yes' the program
exits, if the user enter 'no' or any
// other string, the program will
execute again.
cout<<"exit program(yes/no):
";
cin >> s;
}while(s!="yes");
}
/* OUTPUT SCREEN */
/* PLEASE UPVOTE */