/* ************************************************************************** * Program name : 037_Exponentiation(Recursion) (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/25 - first version * ************************************************************************** * Description : (a) input the valus of base amount * * (b) input the value of exponent * * (c) find the result by "for" loop * * (d) find the result by "while" loop * * (e) try again ? * ************************************************************************** */ #include #include float Exp(float Baseamount, int Topower) { float Result; if (Topower == 1) Result = Baseamount; else Result = Baseamount*(Exp(Baseamount, Topower-1)); return Result; } int main() { // part 1 : declaration float Baseamount, Result; int Topower, Index; char again; do { // part 2 : Repeat asking the valus of base amount do { cout << "Please input the base amount: "; cin >> Baseamount; if (Baseamount<=0) cout << "The base amount must be greater than zero (o)\n"; cout << "\n"; } while (Baseamount<=0); // Note (1) // part 3 : Repeat asking the value of exponent do { cout << "Please input the exponent: "; cin >> Topower; if (Topower<=0) cout << "The exponent must be greater than zero (o)\n"; cout << "\n"; } while (Topower<=0); // Note (2) // part 4 : Find the result Result = Exp(Baseamount,Topower); cout << "The value of " << Baseamount << " to the power " << Topower << " is " << Result << "\n\n"; // part 5 : try another number ? cout << "\aTry another number (Y/N) : "; cin >> again; cout << "\n"; } while (again=='Y' || again=='y'); cout << "\n" << endl; system("PAUSE"); return 0; } /* Notes (1) This program does not support negative number. (2) Any number to the power zero is ONE. This program only works for these exponent is positive integer. */