C Program to Calculate the Power of a Number

Here is a C program to calculate the power of a number

				
					#include <stdio.h>

int main() {
  // Declare variables
  int base, exponent, result;

  // Get input from user
  printf("Enter base: ");
  scanf("%d", &base);

  printf("Enter exponent: ");
  scanf("%d", &exponent);

  // Calculate power
  result = 1;
  for (int i = 0; i < exponent; i++) {
    result *= base;
  }

  // Print result
  printf("%d to the power of %d is %d\n", base, exponent, result);

  return 0;
}

				
			
Output
C Program to Calculate the Power of a Number
  • This program first declares three variables: base, exponent, and result. The base variable stores the base number, the exponent variable stores the exponent, and the result variable stores the result of the calculation.

  • The program then gets input from the user for the base number and the exponent.

  • The program then calculates the power using a for loop. The for loop iterates exponent times, and each time it multiplies result by base.

  • The program then prints the result to the console.

  • You can change the values of the variables in the program to calculate the power of different numbers.