Ejemplo 1: programa factorial c usando el bucle for
#include<stdio.h>
int main(){
int i,f=1,num;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
f=f*i;
printf("Factorial of %d is: %d",num,f);
return 0;
}
Ejemplo 2: factorial de un número dado en c
//Factorial of a given number
#include <stdio.h>
//This function returns factorial of the number passed to it
long int factorialOf(int number){
long int factorial = 1;
while(number){
factorial*=number;
number-=1;
}
return factorial;
}
int main(void) {
int n;
printf("Find factorial of n");
scanf("%d",&n);
printf("nThe factorial of %d is %ld",n,factorialOf(n));
return 0;
}
Ejemplo 3: programa para calcular factorial de número en 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;
}
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)