Recursive Factorial

#include<iostream>
using namespace std;

/* Recursive function */
long int factorial(long int no) {
	if(no == 1)
		return 1;
	else
		/* Call the factorial function inside in it */
		return(no * factorial(no - 1));
}

int main() {
	long int cByV, no;
	cout << "Program to calculate factorial" << endl;
	cout << "Enter a number:" << endl;
	cin >> no;

	/* Call factorial function by passing long integer value and receive result in 'cbyv' variable */
	cByV = factorial(no);
	cout << "Factorial: " << cByV << endl;
	return 0;
}


/* Output */
Program to calculate factorial
Enter a number:
5

Factorial: 120

Comments (0)

  • To add your comment please or

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.

Got It!