How to calculate factorial of a number in C

2 Answers

0 votes
#include <stdio.h>
 
int main(void)
{
    int n, i, fact = 1;
 
    printf("Enter a number: ");
    scanf("%d", &n);
    
    for (i = 2; i <= n; i++)
        fact = fact * i;
        
    printf("Factorial of %d is: %d\n", n, fact);

    return 0;
}
/*
run:

Enter a number: 5
Factorial of 5 is: 120

*/


answered May 7, 2014 by avibootz
edited May 7, 2015 by avibootz
0 votes
#include <stdio.h> 

long factorial(int);

int main(int argc, char **argv) 
{
    int n;
  
    printf("Enter a number: ");
    scanf("%d", &n);
         
    printf("Factorial of %d is: %ld\n", n, factorial(n));

    return(0);
}
long factorial(int n)
{
  int i, fact = 1;
 
  for (i = 2; i <= n; i++)
        fact = fact * i;
 
  return fact;
}


/*
run:

Enter a number: 5
Factorial of 5 is: 120

*/ 


answered May 7, 2015 by avibootz
edited Jul 31, 2015 by avibootz

Related questions

1 answer 243 views
2 answers 159 views
2 answers 232 views
2 answers 278 views
1 answer 124 views
1 answer 194 views
194 views asked Jan 9, 2022 by avibootz
...