- The program first declares three variables:
base
,exponent
, andresult
initialized to 1. - It then prompts the user to enter the base and exponent using the
printf()
andscanf()
functions. - A
for
loop is used to calculate the power of the number. The loop starts withi
equal to 1 and continues untili
is less than or equal toexponent
. In each iteration of the loop,base
is multiplied byresult
. - Finally, the program prints the result using the
printf()
function. - Note that the
result
variable 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