Question

In: Computer Science

C++ Programa that: // 1) Ask the user to enter two numbers greater than zero. Store...

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.

Solutions

Expert Solution

#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 */


Related Solutions

C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that in array. Display that all numbers before and after performing Bubble sort. You must have to create new function with required parameter to perform Bubble sort. Sample Run :- Enter 1 number :- 1 Enter 2 number :- 5 Enter 3 number :- 7 Enter 4 number :- 45 Enter 5 number :- 90 Enter 6 number :- 6 Enter 7 number :- 55...
in MIPS assembly, ask a user for two numbers and to either enter a '1' for...
in MIPS assembly, ask a user for two numbers and to either enter a '1' for addition or '2' for subtraction. using jal, jump to the appropriate procedure for additing or subtracting. then print out result and ask if there is another calculation (terminate program if 'no').
using MIPs assembly, ask the user to enter two numbers. Then multiply the two numbers using...
using MIPs assembly, ask the user to enter two numbers. Then multiply the two numbers using bit shifting (not 'mul'). This should work multiplying 2 positive numbers, 2 negative numbers, and a positive/negative number. Then output the result and ask user if he/she has more calculations to complete.
Write a MIPS program that will ask the user to enter two numbers at the console...
Write a MIPS program that will ask the user to enter two numbers at the console and pass the values to a function that does multiplication
Write a C++ code to perform the following steps: 1. Ask the user to enter two...
Write a C++ code to perform the following steps: 1. Ask the user to enter two whole numbers number1 and number2 ( Number 1 should be less than Number 2) 2. calculate and print the sum of all even numbers between Number1 and Number2 3. Find and print all even numbers between Number1 and Number2 4. Find and print all odd numbers between Number1 and Number2 5. Calculate and print the numbers and their squares between 2 and 20 (inclusive)...
Write a C++ code to perform the following steps: 1. Ask the user to enter two...
Write a C++ code to perform the following steps: 1. Ask the user to enter two whole numbers number1 and number2 (Number1 should be less than number2) 2. Calculate and print the sum of all even numbers between Number1 and Number2 3. Find and print all even numbers between Number1 and Number2 4. Find and print all odd numbers between Number1 and Number2 5. Calculate and print the numbers and their squares between 1 and 20 (inclusive) 6. Calculate and...
I need to ask a user what numbers they want to enter. They can enter as...
I need to ask a user what numbers they want to enter. They can enter as many as they like. Then inside I need to use conditionals to determine if the numbers are <=20, <=323 && > 30, >200. I can't figure out how to let the user enter as many inputs as they want. I know I need to use a loop to check each number entered and determine if it is small middle or greater but I can't...
Write a C++ program to allow a user to enter in any positive number greater than...
Write a C++ program to allow a user to enter in any positive number greater than or equal to zero. The program should not continue until the user has entered valid input. Once valid input has been entered the application will determine if the number is an abundant number or not and display whether or not the number is an abundant number. If the user enters in a 0 the program should quit. An abundant number is a number n...
[C#] Randomly generate two numbers that user chooses and then ask the user what the answer...
[C#] Randomly generate two numbers that user chooses and then ask the user what the answer is when some operator is applied. If user is correct, they will be congratulated. If they are wrong, they will be given the correct answer. This should be repeated based on how many times is chosen by user. I have the code which will do the following below: User wants to know the answer to x % y, What is smallest value of x:...
In MatLab 1. Ask the user to enter a 1 x 5 vector of numbers. Determine...
In MatLab 1. Ask the user to enter a 1 x 5 vector of numbers. Determine the size of the user entry. 2. Ask the user to enter a 1 x 5 vector of numbers. Determine the size of the user entry. If the user enters a 5 x 1 vector, use a warning statement to inform the user of their error, and set the new input value to be the transpose of the user input value. If the user...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT