#include <stdio.h>
int main() {
int base, exponent, result = 1;
// Input the base and exponent from the user
printf("Enter the base: ");
scanf("%d", &base);
printf("Enter the exponent: ");
scanf("%d", &exponent);
// Calculate base raised to the power of exponent
for (int i = 1; i <= exponent; i++) {
result *= base; // Multiply base 'exponent' times
}
// Output the result
printf("%d raised to the power of %d is: %d\n", base, exponent, result);
return 0; // Indicate that the program finished successfully
}
Explanation:
User Input: The program asks the user to input a base and an exponent.Power Calculation: It uses a for
loop to multiply the base by itself the number of times specified by the exponent.
- For example, if base = 2 and exponent = 3, the loop multiplies
2 * 2 * 2
to get 8.
Output: The program prints the result in the format: base raised to the power of exponent is result
.
Output:
Enter the base: 2
Enter the exponent: 3
2 raised to the power of 3 is: 8