Answers for "write a program to find factorial of a number using function in c++"

C
1

program to calculate factorial of number in c++

#include <iostream>
using namespace std;

int main()
{
    unsigned int n;
    unsigned long long factorial = 1;

    cout << "Enter a positive integer: ";
    cin >> n;

    for(int i = 1; i <=n; ++i)
    {
        factorial *= i;
    }

    cout << "Factorial of " << n << " = " << factorial;    
    return 0;
}
Posted by: Guest on January-22-2021
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 program to find factorial of a number using function in c++

#include<stdio.h> 
int factorial(int x);
int main(){
    int a ;
    printf("Enter a value \n");
    scanf("%d",&a);
    printf("The value of factorial of %d is %d",a,factorial(a));
    return 0;
}
int factorial(int x){
    printf("Calling factorial %d\n",x);
    if (x==1 || x==0){
        return 1;
    }
    else{
        return x *factorial(x-1);
    }
}
//This is written using recursion in c
Posted by: Guest on September-15-2021

Code answers related to "write a program to find factorial of a number using function in c++"

Code answers related to "C"

Browse Popular Code Answers by Language