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

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...
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...
Make a program that lets the user enter a positive or negative integer as many times...
Make a program that lets the user enter a positive or negative integer as many times as they want. java //import statement //class header //Begin class scope //Method header for main. //Begin method scope. //Declare input object for Scanner. //Declare variable called entry initialized to zero. //Declare variable called response initialized to blank space. //Prompt "Do you want to enter an integer? Y or N: " //Store value in correct variable.   //while header that tests uppercase in response //Begin scope...
Write a new program named Bar that prompts the user to enter a positive integer. The...
Write a new program named Bar that prompts the user to enter a positive integer. The program should then display a line consisting of the entered number of asterisks using a while loop. If the user enters a number that is not positive, the program should display an error message (see example below). Example 1: Enter a positive number: 6 ****** Example 2: Enter a positive number: 11 *********** Example 3: Enter a positive number: -4 -4 is not a...
PYTHON Let n denote an integer entered by the user. Write a program to print n...
PYTHON Let n denote an integer entered by the user. Write a program to print n multiples of 5 in the descending order, with the last number being 5. Print the average of those n multiples
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT