UMBC CS 201, Spring 02
UMBC CMSC 201 Spring '02 CSEE | 201 | 201 S'02 | lectures | news | help

factorial

The Task

The Program

/*********************************************************** * File: fact.c * Author: S. Bogar * Date: 1/1/77 * SSN: 000-00-0001 * Section: 0101 * EMail: bogar@cs.umbc.edu * * This program includes the Factorial function and * a test program that prints the factorials of the * numbers between the limits LOWER and UPPER, * inclusive. ***************************************************/ #include <stdio.h> /* Constants */ #define LOWER 0 /* starting value */ #define UPPER 7 /* final value */ /* Function prototypes */ int Factorial (int n); /* Main program */ int main() { int i; for (i = LOWER; i <= UPPER; i++) { printf("%d! = %5d\n", i, Factorial(i)); } return 0; } /************************************************** * Function: Factorial() * Usage: f = Factorial(n); * * Inputs: integer for calculating n! * Output: Returns the factorial of the argument n (n!), where * n-factorial is defined as the product of all * integers from 1 up to and including n. ***************************************************/ int Factorial (int n) { int product, i; product = 1; for (i = 1; i <= n; i++) { product *= i; } return (product); }

The Sample Run

0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040

The Lesson


CSEE | 201 | 201 S'02 | lectures | news | help

Thursday, 17-Jan-2002 13:51:56 EST