Advertisement
Kl43z

Factorial

Sep 27th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.62 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. double factorial(int x);
  4. int main(){
  5.    int num;
  6.    printf ("Ingrese un número para calcular su factorial: \n");
  7.    scanf ("%d", &num);
  8.    if (num>=1){  
  9.       printf ("El factorial de %d corresponde a : %.0f\n", num, factorial(num));
  10.    }
  11.    else if (num==0) {
  12.       printf ("El factorial de %d corresponde a : 1\n", num);
  13.    }
  14.    else{
  15.       printf ("No ingresó un número válido.\n");
  16.    }
  17.    return 0;
  18.  
  19.  
  20.  
  21. }
  22.  
  23.  
  24. double factorial(int x){
  25.    int i,temp;
  26.    double fact;
  27.    temp=x-1;
  28.    fact=x;
  29.    for(i=temp;i>=1;i--){
  30.       fact*=temp;
  31.       temp-=1;
  32.    }
  33.    return fact;
  34.    
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement