/* ************************************************************************** * Program name : 018_Factorial_and_Sum (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/10 - 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 void factorial(); // Note (1) void sum (); int main() { // part 1 : declaration char Option; do { cout << "\n" // Note (2) << " [A] = Factorial\n" << " [B] = Sum of the first n integer numbers\n" << " [Q] = Quit\n\n" << "Please enter your selection (A, B or Q) : "; cin >> Option; if (Option=='A' || Option=='a') // Note (3) factorial(); else if (Option=='B' || Option=='b') sum(); } while (Option != 'Q' && Option != 'q'); // Note (4) cout << "\n" << endl; system("PAUSE"); return 0; } // End of Main void factorial() // Note (5) { // part 2a : Repeat asking a nonnegative integer number unsigned long Number, Factorial, Index; do { cout << "Please input a nonnegative integer number: "; cin >> Number; if (Number<=0) cout << "The number must be greater than zero (o)\n"; cout << "\n"; } while (Number<=0); // part 2b : Calculation and output // Note (6) Factorial = 1; for (Index=1; Index<=Number; Index++) Factorial *= Index; cout << "\n The factorial of " << Number << " is " << Factorial << endl; } // end of function factorial void sum () // Note (7) { // part 3a : Repeat asking a nonnegative integer number int Number, Sum, Index; do { cout << "Please input a nonnegative integer number: "; cin >> Number; if (Number<=0) cout << "The number must be greater than zero (o)\n"; cout << "\n"; } while (Number<=0); // part 3b : Calculation and output // Note (8) Sum = 0; for (Index=1; Index<=Number; Index++) Sum += Index; cout << "\n The sum of first " << Number << " numbers is " << Sum << endl; }; // end of function sum /* Notes (1) The statements "unsigned long factorial();" and "int sum ();" are function prototypes. Unlike Pascal, the body of the function is not placed before the main body of the program. The syntex of a function prototypesis : return-value-type function-name(parameter-list); (2) Displays a selection menu. (3) The program will calls the function factorial or sum depend on the value of "Option". (4) The program will stop when the value of "option" is 'Q' or 'q'. (5) The body of the function factorial. No semi-colon ( ; ) is placed at the end of line. This function does not take any arguments and does not return a value. This function works like a procedure in Pascal. (6) Calculation the Factorial of an integer number Example : 4! = 1 * 2 * 3 * 4 = 24 (7) The body of the function sum. No semi-colon ( ; ) is placed at the end of line. This function does not take any arguments and does not return a value. This function works like a procedure in Pascal. (8) Calculation the sum of first 'n' integer numbers Example : sum of first 4 integer = 1 + 2 + 3 + 4 = 10 */