IHYPRESS PROGRAMMING
Tutorials and C programs with code and output for beginners
c programming
HOME | ASP | C | CSS | GNUPLOT | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
CSimple Functions : Factorial Calculator
<05.04>
/* 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 */
Hergestellt in Deutschland / Made in Germany
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 © 2015-2024 IHY PRESS Frankfurt am Main 60329 Deutschland