Question

C++ only Implement a program that prompts a user to enter a number N where N...

C++ only

Implement a program that prompts a user to enter a number N where N is a positive number. The program must validate a user input and ask a user to re-enter until an input is valid. Implement a function that pass N as an argument and return a result of calculates N! (N-factorial):

The function must use loop for the implementation.

Hint: Factorial: N! = N * (N-1) * (N-2) * …. * 1 ( This is only a hint of how to calculate the factorial not the output)

The output of the program must be exactly match the example.

Examples: Enter an integer: 7
7! = 5040

Examples: Enter an integer: 6
6! = 720

Examples: Enter an integer: -2

An input must be positive. Please try again!

Enter an integer: 4

4! = 24

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int Factorial(int n){
   if(n<0){
      return 0;
   }
   else if(n <= 1){
      return 1;
   }
   return n*Factorial(n-1);
}

int main()
{
   int n = -1;
   while(n<0){
      cout << "Enter a number: ";
      cin >> n;
      if(n<0){
         cout<<"An input must be positive. Please try again!"<<endl;
      }
   }
   cout << n <<"! = " << Factorial(n) << endl;
   return 0;
}

Note: Please up vote my answer if it helped.

Add a comment
Know the answer?
Add Answer to:
C++ only Implement a program that prompts a user to enter a number N where N...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ please (1) Write a program that prompts the user to enter an integer value N...

    c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...

  • C++ Write a program that prompts the user to enter two positive integers: num1 and num2....

    C++ Write a program that prompts the user to enter two positive integers: num1 and num2. - Validate that num1 is less than num2 and that both numbers are positive. If any of these conditions are not met, allow the user to re-enter num1 and num2 until the input is determined valid. - For all integers from num1 through num2, print the word keyboard if the current integer is divisible by 2 and print the word mouse if the current...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • 1. Palindrome Write a program that prompts the user for a positive integer number and output whet...

    Language is C 1. Palindrome Write a program that prompts the user for a positive integer number and output whether the number is a palindrome or not. A palindrome number is a number that is the same when reversed. For example, 12321 is a palindrome; 1234 is not a palindromoe. You will write a function named as palindrome. The function will have two arguments, number and isPalindrome. Both arguments are pass-by-reference. The argument number is a pointer to a variable...

  • write in C++ Exercise#1: Is it a Prime? Write a program that prompts the user to...

    write in C++ Exercise#1: Is it a Prime? Write a program that prompts the user to enter a positive integer in the range [100, 100000]. If the integer is not in the specified range the program prompts the user again. The program prints whether the integer is a prime number or not. Sample input/output nter an integer in the range [10e, 10eeee]: 39 nter an integer in the range [100, 100000]: 120453 Enter an integer in the range [10e, 10e000e]:...

  • USING PYTHON - Write a program that calls a function that prompts the user to enter...

    USING PYTHON - Write a program that calls a function that prompts the user to enter a real number. The function should verify that the user entered a valid real number, and should prompt the user to re-enter any invalid input. After a valid real number has been entered, the function should return the number as a number. To test your function, from a main function, call the function two separate times and print the sum of the two numbers...

  • Implement a C++ program that lets the user enter a number and then prints the result...

    Implement a C++ program that lets the user enter a number and then prints the result calculated by the following function. Write a recursive function that computes n * n/3 * n/9 * n/27 * n/81... where n is a non-negative integer, n/x is integer division and the quotient is always a multiple of 3 from the previous quotient. The sequence repeats as long as n/x > 0. For example: 27: 27 * 9 * 3 * 1 = 729...

  • in Python: Write a program that allows the user to enter a number between 1-5 and...

    in Python: Write a program that allows the user to enter a number between 1-5 and returns an animal fact based on what the user entered. The program should 1. Continue to run until the user enters “quit” to terminate the program, and 2. Validate the user’s input (the only acceptable input is 1, 2, 3, 4, 5 or “quit”). You can use the following animal facts: An octopus is a highly intelligent invertebrate and a master of disguise. Elephants...

  • C++ only Write a program that prompts the user for three integers, find the maximum number,...

    C++ only Write a program that prompts the user for three integers, find the maximum number, their summation and their difference and prints it out to the console. The program must contain at least 3 functions: 1. A finding maximum function: The function will accept three inputs and return the maximum number. 2. A summation function: The function will accept three inputs and return the summation of all three numbers. 3. A subtraction function: The function will accept three inputs...

  • C++ Font Exercise #5: Multiples of a Number Write a C++ program that prompts the user...

    C++ Font Exercise #5: Multiples of a Number Write a C++ program that prompts the user to enter three integers, n, m, and k, where n is the lower limit, m is the upper limit of a range of positive numbers, and k is any positive number. The program then prints all multiples of the number k between n and m. Treb Sample input/ output: Charac nter the lower and upper limits: 12 93 Enter the multiple: 7 the multiples...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT