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...
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...
[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...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code should print the numbers in descending order.
(1) Prompt the user to enter five numbers, being five people's weights. Store the numbers in...
(1) Prompt the user to enter five numbers, being five people's weights. Store the numbers in an array of doubles. Output the array's numbers on one line, each number followed by one space. (2 pts) Ex: Enter weight 1: 236.0 Enter weight 2: 89.5 Enter weight 3: 142.0 Enter weight 4: 166.3 Enter weight 5: 93.0 You entered: 236.0 89.5 142.0 166.3 93.0 (2) Also output the total weight, by summing the array's elements. (1 pt) (3) Also output the...
Inside a do while loop ask a user to enter two numbers (use a single scanf...
Inside a do while loop ask a user to enter two numbers (use a single scanf function). Inside the same loop ask the user whether they wish to find the min or max of the values entered. If the user enters a 0 call the minFunction; if the user enters a 1 call the maxFunction; otherwise print Not valid input. Pass the two first numbers entered as parameters to the functions. Inside minFunction and maxFunction calculate the minimun and the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT