Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop p

c++ program to find factorial of a number

to find factorial of a number

The calculation of factorial in programming is also an example of recursion. But the recursion isn't memory efficient if you don't care about this, It is a choice to save time.

The factorial of any number n > 1 is n multiplied by the factorial of n - 1. See the implementation.

Example

fact(4)

4 * fact(3)

4 * 3 * fact(2)

4 * 3 * 2 * fact(1)

The factorial of a non-negative integer n is equal to the product of all positive integers from 1 to n inclusively. Also by definition, the factorial of 0 is 1.

Code snippet: C++ program to find factorial of n


#include <iostream >
using namespace std;

// program to calculate factorial of a num ->>>>
int factorial(int n)
{
if (!(n > 1 ) || n == 0)
return 1;
else
return n * factorial(n-1);
}

int main() {

cout << "Enter the value of N : ->> " << endl; int N; cin>> N;

cout<< "factorial of N is : ->> " << factorial(N); return 0; }

No comments:

Post a Comment