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 :: c++ access second last element of vector 
Cpp :: how to concatenate two vectors in c++ 
Cpp :: transformer in nlp 
Cpp :: create matrix cpp 
Cpp :: for loop in cpp 
Cpp :: resize string c++ 
Cpp :: how to create a file in c++ 
Cpp :: log base 2 in c++ 
Cpp :: c++ program to find lcm of two numbers 
Cpp :: template c++ 
Cpp :: search by value in map in stl/cpp 
Cpp :: passing custom function in sort cpp 
Cpp :: sort strings by length and by alphabet 
Cpp :: find pair with given sum in the array 
Cpp :: how to take full sentence in c++ 
Cpp :: fill two dimensional array c++ 
Cpp :: how to convert hexadecimal to decimal in c++ 
Cpp :: vector<intv[] 
Cpp :: c++ regex count number of matches 
Cpp :: square gcode 
Cpp :: opengl draw house using glut c++ 
Cpp :: modular exponentiation algorithm c++ 
Cpp :: backtrack 
Cpp :: how to find size of int in c++ 
Cpp :: cpp vector structure 
Cpp :: c++ concatenate strings 
Cpp :: C++ Vector Operation Change Elements 
Cpp :: operator overloading c++ 
Cpp :: cout in c++ 
Cpp :: C++ Initialization of three-dimensional array 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =