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
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
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;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us