Factiorial with function

nik96 (4)
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 . ....
tvrameshmc (54)
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.
nik96 (4)
thank you so much for the answer..!!
nik96 (4)
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
tvrameshmc (54)
1
2
3
4
5
6
 int input;
   cout << "\nEnter number to calculate factorial:";
   cin >> input;
   
   int fact=factorial(input);
   cout<<fact;

vlad from moscow (3655)
@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
nik96 (4)
What is the full code?
vlad from moscow (3655)
@nik96
What is the full code?



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

Registered users can post here. Sign in or register to post.