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

how to find factorial of number in c++

#include <iostream>
using namespace std;

int main() {
    int n;
    long factorial = 1.0;

    cout << "Enter number: ";
    cin >> n;

    if (n < 0)
        cout << "Error! Factorial of a negative number doesn't exist.";
    else {
        for(int i = 1; i <= n; ++i) {
            factorial *= i;
        }
        cout << "Factorial of " << n << " = " << factorial;    
    }

    return 0;
}
Comment

find factorial in c++ using class

#include <iostream>
using namespace std;

class Factorial
{
private:
    int num;
    int factorial = 1;

public:
    void calculateFactorial();
    void show();
};

void
Factorial::calculateFactorial()
{
    cout << "Enter a number : " << endl;
    cin >> num;

    if (num == 0 || num == 1)
    {
        factorial = 1;
    }
    else
    {
        while (num > 1)
        {
            factorial = factorial * num;
            num--;
        }
    }
}

void Factorial::show()
{
    cout << "Factorial : " << factorial << endl;
}

int main()
{
    Factorial factorial;
    factorial.calculateFactorial();
    factorial.show();
}
Comment

how to factorize a number in c++

// C++ program to print all prime factors
#include <bits/stdc++.h>
using namespace std;
 
// A function to print all prime
// factors of a given number n
void primeFactors(int n)
{
    // Print the number of 2s that divide n
    while (n % 2 == 0)
    {
        cout << 2 << " ";
        n = n/2;
    }
 
    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n
        while (n % i == 0)
        {
            cout << i << " ";
            n = n/i;
        }
    }
 
    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        cout << n << " ";
}
 
/* Driver code */
int main()
{
    int n = 315;
    primeFactors(n);
    return 0;
}
 
// This is code is contributed by rathbhupendra
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ remove numbers from vector if larger than n 
Cpp :: delete from front in vector c++ 
Cpp :: string.begin() c++ 
Cpp :: how to get size of 2d vector in c++ 
Cpp :: tolower funciton in cpp 
Cpp :: lutris 
Cpp :: clear qlayout 
Cpp :: how to initialize array with new in c++ 
Cpp :: sort vector in reverse order c++ 
Cpp :: for c++ 
Cpp :: map declaration c++ 
Cpp :: iterate over vector in c++ 
Cpp :: c++ function default argument 
Cpp :: how to append to a vector c++ 
Cpp :: how to empty an array c++ 
Cpp :: memmove 
Cpp :: how to compile opencv c++ in ubuntu 
Cpp :: c++ get the line which call a function 
Cpp :: substr in cpp 
Cpp :: c pre-processor instructions 
Cpp :: c++ char array size 
Cpp :: c++ Program to check if a given year is leap year 
Cpp :: length of number c++ 
Cpp :: inline c++ 
Cpp :: stl c++ 
Cpp :: why convert char* to string c++ 
Cpp :: length of a string c++ 
Cpp :: data types in c++ 
Cpp :: c++ template vs code 
Cpp :: google test assert stdout 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =