- The program first declares three variables:
base,exponent, andresultinitialized to 1. - It then prompts the user to enter the base and exponent using the
printf()andscanf()functions. - A
forloop is used to calculate the power of the number. The loop starts withiequal to 1 and continues untiliis less than or equal toexponent. In each iteration of the loop,baseis multiplied byresult. - Finally, the program prints the result using the
printf()function. - Note that the
resultvariable is initialized to 1 before the loop starts because any number raised to the power of 0 is 1.
#include <stdio.h>
int main() {
int base, exponent, result = 1;
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
for (int i = 1; i <= exponent; i++) {
result *= base;
}
printf("%d^%d = %d", base, exponent, result);
return 0;
}
OUTPUT
Enter base: 2
Enter exponent: 5
2^5 = 32






