Skip to content

Instantly share code, notes, and snippets.

@mbalayil
Created May 13, 2011 11:02
Show Gist options
  • Save mbalayil/970350 to your computer and use it in GitHub Desktop.
Save mbalayil/970350 to your computer and use it in GitHub Desktop.
To find the factorial of a number in C
/**
* To find the factorial of a number
**/
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int n, i, f;
printf("Enter any number:");
scanf("%d", &n);
if(n < 0) {
printf("\nFactorial does not exist for negative numbers\n");
exit(0);
}
for(f = 1, i = n; i > 0; i--)
f = f * i;
printf("\nFactorial of %d is %d\n", n, f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment