Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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

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

PREVIOUS NEXT
Code Example
Cpp :: concatenate string in cpp 
Cpp :: dangling pointer 
Cpp :: operator overloading in c++ 
Cpp :: c++ function pointer variable 
Cpp :: c++ else if 
Cpp :: c++ switch case 
Cpp :: c++ copy constructor 
Cpp :: tr bash 
Cpp :: enum in c++ 
Cpp :: files c++ 
Cpp :: min heap 
Cpp :: && c++ 
Cpp :: c++ visual studio 
Cpp :: c++ map vector as keys 
Cpp :: online converter c++ to c 
Cpp :: flipperRSocketResponder.cpp command failed react native 
Cpp :: c++ the hash function with 31 const 
Cpp :: even number program in c++ using for loop stack overflow 
Cpp :: c++ graphics online compiler 
Cpp :: how you can add intger value to string in c++ 
Cpp :: turbo c++ easy programs 
Cpp :: selection sort algorithm in cpp 
Cpp :: coin change top-down 
Cpp :: I/O Redirection in C++ 
Cpp :: nothrow new in cpp 
Cpp :: entering char in int c++ avoid loop 
Cpp :: C++ initializing a thread with a public function of a class 
Cpp :: solve diamond inheritance c++ 
Cpp :: c++ merging algorithm 
Cpp :: what is vector capacity in c++ 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =