IHYPRESS.NET > C PROGRAMMING
HOME | CLASSIC ASP | C | HTML/CSS | JAVASCRIPT | PERL | PHP | PYTHON | RUBY
C > 05. Functions I [PROGRAM #04]
CODERESULT
<
/* The factorial function: one argument, one result */
#include <stdio.h> /* this function will return an integer */ int factorial (int n) { int i, product; product = 1; /* initialization */ /* computes n*n-1... */ for (i=n; i>1; i=i-1) { product = product * i; } /* the value that goes out */ return (product); } int main (void) { int a, result; printf ("Enter an integer number: "); scanf ("%d", &a); /* calling the function */ result = factorial (a); /* printing the report */ printf ("The factorial of %d is %d.\n", a, result); return (0); } /* NOTE: a large number (like 13) will produce an arithmetic overflow */
Legend: preprocessor directives | variable declarations main function | helper functions | user-defined structures comments
Enter an integer number: 4
The factorial of 4 is 24.

Enter an integer number: 13
The factorial of 13 is 1932053504.

The factorial of 13 is 6227020800. Error caused by arithmetic overflow.
>
COPYRIGHT © 2021 IHYPRESS.NET. A DIVISION OF IHY PRESS, KIRKLAND, WA 98083 USA. ALL RIGHTS RESERVED.