/* ************************************************************************** * Program name : 014_Exponentiation(Iteration) (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/08 - 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 int main() { // part 1 : declaration float Baseamount, Result; int Topower, Index; char again; do { // part 2 : Repeat asking the valus of base amount until base amount > 0 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 4a : Find the result - method (I) for loop // Note (3) Result = 1; for (Index = 1; Index <= Topower; Index++) Result *= Baseamount; cout << "The value of " << Baseamount << " to the power " << Topower << " is " << Result << "\n\n"; // part 4b : Find the result - method (II) while loop Result = 1; Index = 1; do { Result *= Baseamount; Index++; } while (Index<=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. (3) Both "for" loop or "while" loop works. */