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)...
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++ 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.
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...
(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...
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
In C# When the user enters an invalid value, ask the user to repeatedly enter the...
In C# When the user enters an invalid value, ask the user to repeatedly enter the value until a valid value has been entered. Gender must be ‘M’ or ‘F’. Residency must be ‘I’ or ‘O’. Existing Code: using System; public class Student {   public int credit;   public String firstname, lastname, gender, residency, edate;   public void input()   {     Console.WriteLine("\nWelcome to the Continental University Registration System!"); Console.WriteLine("\nEnter data about a student"); Console.Write("First Name: "); firstname = Console.ReadLine(); Console.Write("Last Name: "); lastname...
for python 3 a. Ask the user to enter 10 numbers. Each number is stored in...
for python 3 a. Ask the user to enter 10 numbers. Each number is stored in a list called myList. Compute and print out the sum and average of the items in the list. Print the numbers that are divisible by 2 from myList. b. Instead of using a list to store the numbers, create a loop to accept the numbers from the user, and find the average of all the numbers. Hint: use an accumulator to store the numbers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT