#include <iostream>
using namespace std;
int main(){
int base, exp, result = 1;
cout<<"Enter the base: ";
cin>>base;
cout<<"Enter the exponent: ";
cin>>exp;
for(int i=1; i<=exp;i++){
result = result * base;
}
cout<<base<<" raised to the power "<<exp<<" is "<<result<<endl;
return 0;
}
Output: Here's how this program works:
1. The program starts by including the standard input/output library, iostream, and using the std namespace.
2. The main function is declared, which returns an integer value and takes no arguments.3. Three integer variables are declared: base, exp, and result. The result variable is initialized to 1.
4. The program prompts the user to enter the base and exponent values.
5. The user inputs the base and exponent values using the cin (console input) command.
6. A for loop is initiated with a loop counter variable, i, starting at 1 and going up to exp.
7. Within the loop, the result variable is updated by multiplying it with the base value.
After the loop completes, the program outputs the result of the computation using cout (console output).
8. The program ends by returning the integer value 0 to the operating system.
No comments:
Post a Comment