Friday, May 13, 2011

Factorial of a number in C - Iterative algorithm

/**
* 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;
}
view raw factorial.c hosted with ❤ by GitHub
Written by

No comments:

Post a Comment