Factiorial with function

i need help to calculate the factorial with function

The program should then calculate the factorial of the
number n, where n!= n×(n −1)...× 2×1

the output like this:

Enter number: 4
Factorial of 4! = 24
Enter number: 6
Factorial of 6! = 720
Enter number: 3
Factorial of 3! = 6
Enter number: 0
Factorial of 0! = 1
Enter number: -5
Factorial of -5! = -1
Press any key to continue . ....
1
2
3
4
5
6
7
int factorial(int n)
{
	if(n<=0)
		return 1;
	else
		return n*factorial(n-1);
}


This is recursive function to calculate factorial of a number.
thank you so much for the answer..!!
and call two function the first asked the user to type a number use one for loop and second used
1
2
3
4
5
6
7
[int factorial(int n)
{
	if(n<=0)
		return 1;
	else
		return n*factorial(n-1);
}
this to calculate the factorial is correct?
Last edited on
1
2
3
4
5
6
 int input;
   cout << "\nEnter number to calculate factorial:";
   cin >> input;
   
   int fact=factorial(input);
   cout<<fact;

@tvrameshmc

1
2
3
4
5
6
7
int factorial(int n)
{
	if(n<=0)
		return 1;
	else
		return n*factorial(n-1);
}


The code does not correspond to the original assignment because for negative numbers the function shall return -1.
Last edited on
What is the full code?
@nik96
What is the full code?



It is your task to write the full code. One of possible realization was already demonstrated.

Topic archived. No new replies allowed.