Search
 
SCRIPT & CODE EXAMPLE
 

CPP

FACTORIAL IN C++

#include <iostream>  
using namespace std;  
int main()  
{  
   int i,fact=1,number;    
  cout<<"Enter any Number: ";    
 cin>>number;    
  for(i=1;i<=number;i++){    
      fact=fact*i;    
  }    
  cout<<"Factorial of " <<number<<" is: "<<fact<<endl;  
  return 0;  
}  
Comment

c++ factorial

#include <cmath>

int fact(int n){
    return std::tgamma(n + 1);  
}    
// for n = 5 -> 5 * 4 * 3 * 2 = 120 
//tgamma performas factorial with n - 1 -> hence we use n + 1
Comment

cpp factorial of a number.

/*#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

built in factorial function in c++

int factorial(int n)
{
  return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
}
Comment

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 loop C++

#include<iostream>

using namespace std;
	
int main(){
    int no = 5;
    
    
    int factorial = 1;
    
    for(int a=no;a>0;--a){     //note: decrement(--)
    factorial *= a ;			// note(*=)
    }
    cout << factorial ;			// 120

}
Comment

PREVIOUS NEXT
Code Example
Cpp :: 3d vector c++ resize 
Cpp :: how to get the time in c++ as string 
Cpp :: methods available for a stl vector 
Cpp :: find kth max and min element in an array 
Cpp :: Palindrome String solution in c++ 
Cpp :: str remove char c++ 
Cpp :: Program To Calculate Number Power Using Recursion In C++. The power number should always be positive integer. 
Cpp :: unordered_set to vector 
Cpp :: substr in cpp 
Cpp :: c++ string find example 
Cpp :: c elif 
Cpp :: doubly linked list in cpp 
Cpp :: how to change colour image to grey in opencv c++ 
Cpp :: zero fill in c++ 
Cpp :: dynamic memory c++ 
Cpp :: show stack c++ 
Cpp :: c++ split string by space into array 
Cpp :: print hello world c++ 
Cpp :: char to string c++ 
Cpp :: c++ client service ros 
Cpp :: length of a string c++ 
Cpp :: vector of threads thread pool c++ 
Cpp :: il2cpp stuck unity 
Cpp :: how to empty a std vector 
Cpp :: Syntax for C++ Operator Overloading 
Cpp :: Find duplicates in an array geeks for geeks solution in cpp 
Cpp :: creating node in c++ 
Cpp :: c++ custom hash 
Cpp :: count number of char in a string c++ 
Cpp :: unordered map c++ 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =