C Program to Calculate the Power of a Number

C Program to Calculate the Power of a Number

Write a Program to Print the Power of a Number.

This program calculates the power of a number using a for loop. The user inputs the base and exponent, and the program computes the result using repeated multiplication.

/*Program to Calculate the Power of a Number*/
#include <stdio.h>
int main()
{
  int num1, num2;
  // Taking input from user
  printf("Enter Base :");
  scanf("%d", &num1);
  printf("Enter Exponent :");
  scanf("%d", &num2);
  
  int power = 1;
  // Calculate Power using for loop
  for (int i = 1; i <= num2; i++)
  {
    power = power * num1;
  }
  // Print the output result
  printf("%d raised to the power %d is: %d", num1, num2, power);
  return 0;
}

Explanation:

 Calculate the Power(x,n):
  x = 4, n = 2
  xn -> 42 -> 16
 

Output:

   Enter Base :4
   Enter Exponent :2
   
   4 raised to the power 2 is: 16
 

*

إرسال تعليق (0)
أحدث أقدم