Question

In: Computer Science

Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as

Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as: n! = n·(n-1)·(n-2)·.……·3·2·1. The factorial of 0 is 1. If a number is negative, factorial does not exist and this program should display error message. Sample output dialogues should look like these: 

Enter an integer: -5

ERROR!!! Tactorial of a negative number doesn't exist 

Enter an integer: 10 

Factorial = 3628800

Solutions

Expert Solution

#include

using namespace std;
int factorial(int n)
{
if(n==0)
return 1;
else
return n*factorial(n-1);
}
int main()
{

cout<<"Enter an integer: ";
int n;
cin>>n;
if(n<0)
cout<<"Error!!! Factorial of a negative number doesn't exist\n";
else
cout<<"Factorial = "<<factorial(n)<<endl;
return 0;
}


Related Solutions

Create a C++ program that will prompt the user to input an positive integer number and...
Create a C++ program that will prompt the user to input an positive integer number and output the corresponding number to words. Check all possible invalid input data. (Please use only switch or if-else statements. Thank you.)
On a raspberry pi, write an assembler program that will calculate the factorial of an integer...
On a raspberry pi, write an assembler program that will calculate the factorial of an integer value inputted by the user. the program should detect if an overflow occurs. If no overflow occurred, then it should print out the value of the factorial. Otherwise print out a message indicating that an overflow occurred.
In javascript, make a program that takes in a positive integer (entered on a textbox and...
In javascript, make a program that takes in a positive integer (entered on a textbox and submitted by clicking a button). Then, it displays the sum of all the integers between that number and zero. <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script> function add(n) {    /*This function MUST USE RECURSION to do the sum of the numbers.*/ } function check() {    /*This function takes the input entered and checks if it's an integer. If it's not a number, it should...
Write a program which: Prompts the user for a positive integer >= 0 Validates the user...
Write a program which: Prompts the user for a positive integer >= 0 Validates the user input to ensure it is a positive integer >= 0 Allocate (dynamically) an array big enough for the data. Load the array with random numbers ranging in value from1 to 100 Display the elements of the array (unsorted) Display the elements of the array (sorted) Display the average Display the median Display the mode, if none, display appropriate message RESTRICTIONS No global variables No...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Write a program which prompts the user for a positive integer, and then prints out the...
Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response. Do not import anything other than the Scanner. One way you might go about this using nested loops: Start a "factor" variable at 2 In a loop: repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor increment the factor This plan is by no stretch...
Write a C++ program that accepts a single integer value entered by user. If the value...
Write a C++ program that accepts a single integer value entered by user. If the value entered is less than one the program prints nothing. If the user enters a positive integer n. The program prints n x n box drawn with * characters. If the user enters 1 , for example the program prints *. If the user enter a 2, it prints ** ** that is , a 2x2 box of * symbols.
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
JAVA PROGRAM 1. Write a program to find the factorial value of any non-negative number entered...
JAVA PROGRAM 1. Write a program to find the factorial value of any non-negative number entered through the keyboard.(method) (Factorial of n: n! = 1*2*3*…*n, 0! = 1.) 2. Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (method) Please post a screenshot of the codes. Thanks!
A. Write a program 1. Prompt the user to enter a positive integer n and read...
A. Write a program 1. Prompt the user to enter a positive integer n and read in the input. 2. Print out n of Z shape of size n X n side by side which made up of *. B. Write a C++ program that 1. Prompt user to enter an odd integer and read in the value to n. 2. Terminate the program if n is not odd. 3. Print out a cross shape of size n X n...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT