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

Write a C function to calculate and return the factorial value of any positive integer as...
Write a C function to calculate and return the factorial value of any positive integer as an argument. Then call this function from main() and print the results for the following input values: 2, 3,4, 5, 10, 15 What is the maximum value of an integer for which factorial can be calculated correctly on a machine using your algorithm?
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...
Create the function fact that had a positive integer argument and returns its factorial . Recall...
Create the function fact that had a positive integer argument and returns its factorial . Recall that factorial (n) = 1*2*3***n. For example factorial (4)=1*2*3*4=24, (python)
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.
Playing with encryption: Write a program that will read a four-digit integer entered by the user...
Playing with encryption: Write a program that will read a four-digit integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the second digit with the fourth. Finally, print the original number and its encrypted one. Now reverse the process. Read an encrypted integer and decrypt it by reversing the algorithm to obtain the original...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT