15 September 2009

factorial in C

int fact(int num)
{
if(num<=0) return 1;

return num * fact(num-1);
}


---------------------factorial.c---------------------
#include <stdio.h>
#include <stdlib.h>

int fact(int num)
{
if(num<=0) return 1;

return num * fact(num-1);
}

int main(int argc, char *argv[])
{
int num = 0;

if(argc < 2)
{
printf("usage: %s <number>\n", argv[0]);
exit(0);
}

num = atoi(argv[1]);

printf("factorial of %d is %d\n", num, fact(num));
return 0;
}
-----------------------------------------------------

No comments:

Post a Comment