Answers for "factorize number cpp"

C++
8

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
Posted by: Guest on July-09-2020
0

Write a C++ program to calculate factorial of a number.

#include <iostream>
using namespace std;

int main() {

    // #ifndef ONLINE_JUDGE // not part of code, used to beautify input output
    //     freopen("input.txt", "r", stdin);
    //     freopen("output.txt", "w", stdout);
    // #endif

    int n, i, fact;
    cin >> n;
    fact = n;

    for (i = 1; i < n; i++) {
        fact = fact * (n - i);
    }

    cout << fact;

    return 0;
}
Posted by: Guest on October-25-2021

Browse Popular Code Answers by Language