home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Sams Teach Yourself C in 21 Days (6th Edition)
/
STYC216E.ISO
/
mac
/
Examples
/
Day05
/
recurse.c
< prev
next >
Wrap
C/C++ Source or Header
|
2002-04-14
|
652b
|
38 lines
/* Demonstrates function recursion. Calculates the */
/* factorial of a number. */
#include <stdio.h>
unsigned int f, x;
unsigned int factorial(unsigned int a);
int main( void )
{
puts("Enter an integer value between 1 and 8: ");
scanf("%d", &x);
if( x > 8 || x < 1)
{
printf("Only values from 1 to 8 are acceptable!");
}
else
{
f = factorial(x);
printf("%u factorial equals %u\n", x, f);
}
return 0;
}
unsigned int factorial(unsigned int a)
{
if (a == 1)
return 1;
else
{
a *= factorial(a-1);
return a;
}
}