/* ************************************************************************** * Program name : 023_Factorial_and_Sum_Recursive_verrsion (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/11 - first version * ************************************************************************** * Description : (a) Get user's selection * * (b) Input a nonnegative integer number * * (c) Calculate the value of Factorial or Sum * * (d) try again ? * ************************************************************************** */ #include #include #include int getnumber(void); // Note (1) unsigned long factorial(int Number); int sum (int Number); int main() { // part 1 : declaration int Number; char Option; do { cout << "\n\n" // Note (2) << " [A] = Factorial (accept 1 to 33 only)\n" << " [B] = Sum of the first n integer numbers\n" << " [C] = Both Factorial and Sum\n" << " [Q] = Quit\n\n" << "Please enter your selection (A, B, C or Q) : "; cin >> Option; switch (Option) // Note (3) { case 'A' : case 'a': Number = getnumber(); cout << "\n\tThe factorial is " << factorial(Number); break; case 'B' : case 'b': Number = getnumber(); cout << "\n\tThe sum is " << sum(Number); break; case 'C' : case 'c': Number = getnumber(); cout << "\n\tThe factorial is " << factorial(Number); cout << "\n\tThe sum is " << sum(Number); break; } } while (Option != 'Q' && Option != 'q'); // Note (4) cout << "\n" << endl; system("PAUSE"); return 0; } // End of Main int getnumber(void) // Note (5) { int Number; do { cout << "\nPlease input a non-negative integer number: "; cin >> Number; if (Number<=0) cout << "The number must be greater than zero (o)\n"; cout << "\n"; } while (Number<=0); return Number; // Note (6) }; unsigned long factorial(int Number) // Note (5) { if (Number <= 1 ) return 1; else return Number * factorial(Number-1); // Note (7) }; // end of function factorial int sum (int Number) // Note (5) { if (Number <= 1 ) return 1; else return Number + sum(Number-1); // Note (8) }; // end of function sum /* Notes (1) The statements "unsigned long factorial(int Number);" and "int sum (int Number);" are function declaration statements. To declare a function is to give the data type of the value that the function returns (or void if the function has no return value), its name, and in parentheses the data types of its parameters separateed by commas(or void if the function has no parameter). The standard style of writing function declaration is : return-value-type function-name (parameter-list); ----------------------------------------------------------------------- int getnumber (void); or (); unsigned long factorial (int Number); or (int); int sum (int Number); or (int); C++ programs do not compile unless a function prototype is provided for every function or a function is defined before it is used. Therefore the body of the function can be placed after the body of the main program with function prototype or before the main program without function prototype. (2) Displays a selection menu. (3) Use the "switch" control statements to call the function factorial and or sum depending on the value of "Option". (4) The program will stop when the value of "option" is 'Q' or 'q'. (5) The define a function is to create it by giving its header and its body. No semi-colon ( ; ) is placed at the end of the header line. return-value-type function-name (parameter-list); --------------------------------------------------------- int getnumber (void) unsigned long factorial (int Number) int sum (int Number) (6) Return a non-negative integer number (Number) to the main program. (7) Calculation the Factorial of an integer number Example : 4! = 1 * 2 * 3 * 4 = 24 (8) Calculation the sum of first 'n' integer numbers Example : sum of first 4 integer = 1 + 2 + 3 + 4 = 10 */