Answers for "factorial c++"

C++
6

factorial in c++

#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;

int fact(int i){
	if (i <= 1) return 1;
  	else return i*fact(i-1);
}

int main(){
  	ios::sync_with_stdio(0);
  	cin.tie(0);
  	int N;
  	cin >> N;
  	cout << fact(N) << "\n";
  	return 0;
}
Posted by: Guest on May-21-2020
1

factorial c++ without using function

#include <iostream>
using namespace std;

int main()
{
	unsigned int num; //unsigned is used for not taking negetive values.
	unsigned long long factorial = 1; //Since the factorial a number can be large, so long long data type is used.
	cout << "Give me any positive number :	";
	cin >> num;

	for (int i = 1; i <= num; i++) {
		factorial = factorial * i;
	}
	cout << "Factorial of the given number is: " << factorial;
}
Posted by: Guest on November-07-2020
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

built in factorial function in c++

int factorial(int n)
{
  return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
}
Posted by: Guest on November-01-2021
0

Factorial en c++ con while

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    // Variables a utilizar
    long int Resultado=0;
    long int Factorial;

    // Solicitar numero para calcular el factorial
    printf("Introduzca No. para calcular el factorial ...: ");
    scanf(" %ld", &Factorial);
    // Calcular el factorial del numero solicitado
    Resultado = 1;
    while(Factorial > 1) {
       Resultado *= Factorial;
       printf(" %ld x",Factorial);
       Factorial--;
    }
    printf(" 1 = %ld\n\n",Resultado);

  system("PAUSE");
  return 0;
}
Copy
Posted by: Guest on October-11-2021

Browse Popular Code Answers by Language