Search
 
SCRIPT & CODE EXAMPLE
 

CPP

factorial using recursion cpp

#include <iostream>
using namespace std;

int fact(int n);

int main()
{
    int n;
    cout<< "Enter the number: ";
    cin>> n;

    cout<< "Factorial of " <<n <<" is " <<fact(n);

}

int fact(int n)

{
    if (n>=1)
        return n*fact(n-1);
    else
        return 1;
}
Comment

factorial in c++ using recursion

#include <iostream>
using namespace std;

int fact (int);
int main()
{
    cout << " Please enter your number = ";
    int n ;
    cin >> n ;

    cout << " Answer is = " << fact (n) << endl;
    return 0;
}

int fact (int n )
{
    if (n <= 1)
        return 1;
    return n * fact (n-1);
}
Comment

cpp factorial of a number recursive

/*#include<bits/stdc++.h>
using namespace std;
iterative solution:
int fact (int n, int k)
{
	if(n==0) return 1;
	return fact(n-1,k*n);
}
int main()
{
	int n,k=1;
	cin>>n;
	int ans=fact(n,k);
	cout<<ans<<endl;
}*/
//recursive solution.
#include<bits/stdc++.h>
using namespace std;
int fact(int n)
{
	if(n==0)return 1;
	return n*fact(n-1);
}
int main(){
	int n;
	cin>>n;
	cout<<fact(n)<<endl;
	
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to check size of file in c++ 
Cpp :: convert int to enum c++ 
Cpp :: how to clear console c++ 
Cpp :: vector to string c++ 
Cpp :: how to find length of character array in c++ 
Cpp :: string to number in c++ 
Cpp :: c++ program to find prime number using function 
Cpp :: cases in cpp 
Cpp :: c++ string comparison 
Cpp :: reverse string c++ 
Cpp :: c++ count number of element in vector 
Cpp :: c++ cin operator 
Cpp :: c++ iterate map 
Cpp :: how to check if a number is prime c++ 
Cpp :: c++ simple car game 
Cpp :: print each number of digit c++ 
Cpp :: c++ segmented sieve primes 
Cpp :: C++ press enter to continue function 
Cpp :: setprecision c++ 
Cpp :: log base 10 c++ 
Cpp :: size of pointer array 
Cpp :: what is c++ used for 
Cpp :: Count Prefix of a Given String solution leetcode 
Cpp :: insert only unique values into vector 
Cpp :: even and odd sum in c++ 
Cpp :: cout c++ 
Cpp :: how to print a text in c++ 
Cpp :: function in c++ 
Cpp :: C++ Limit of Integer 
Cpp :: c++ string split 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =