Search
 
SCRIPT & CODE EXAMPLE
 

CPP

factorial function C++

#include<iostream>

using namespace std;

int factorial(int x){
	if(x==0){               // Base-case   ( VERY IMPORTANT)
		return(1);
	}
	else{
	   return(x*(factorial(x-1))) ;
	}
}
	
int main(){
    int no = 5;
    
    cout << factorial(no) ;    // 120
}
Comment

factorial calculator c++

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll fact(ll a){
    ll ans = 1;
    ll curr = 1;
    while(a--){
        ans *= curr;
        curr++;
    }
    return ans;
}
int main(){
  	int x;
  	cin >> x;
  	cout << fact(x) ;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: classes constructor in c++ 
Cpp :: C++, for-loop over an array array 
Cpp :: tree to array c++ 
Cpp :: copying a set to vector in c++ 
Cpp :: convert integer to string in c++ 
Cpp :: c++ initialize a vector 
Cpp :: cpp string find all occurence 
Cpp :: C++ std::optional 
Cpp :: print counting in c++ 
Cpp :: length of number c++ 
Cpp :: all permutations with repetition C++ 
Cpp :: c++ split string by space into array 
Cpp :: SUMOFPROD2 
Cpp :: c++ range based for loop 
Cpp :: c++ region 
Cpp :: reverse an array in c++ stl 
Cpp :: c++ integer array 
Cpp :: string reverse iterator c++ 
Cpp :: preorder 
Cpp :: CRED Coins codechef solution in c++ 
Cpp :: set of vectors c++ 
Cpp :: c++ remove all elements equal to 
Cpp :: c++ if statement 
Cpp :: c++ check that const char* has suffix 
Cpp :: how to pass an array by reference in c++ 
Cpp :: flag of georgia 
Cpp :: shortest path in unweighted graph bfs 
Cpp :: #define in cpp 
Cpp :: dangling pointer in cpp 
Cpp :: phi function (n log (log(n))) 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =