C Program to Find the Power of a Number

C program to find or calculate power of a number; In this tutorial, i am going to show you how to find or calculate power of a number in the c program using for loop, while loop, and function.

Algorithm and Programs to Find the Power of a Number in C

  • Algorithm to Find the Power of a Number
  • C Program to Find the Power of a Number using Pow() Function
  • C Program to Find the Power of a Number using For Loop
  • C Program to Find the Power of a Number using While Loop

Algorithm to Find the Power of a Number

Follow the below given algorithm to write a program to find power of a number; as follows:

  • Step 1: Start Program
  • Step 2: Read the base and exponent number from user and store into it in variables
  • Step 3: Find the power of a number using the for loop or while loop or pow() function
  • Step 4: Print power of a number
  • Step 5: Stop Program

C Program to Find the Power of a Number using Pow() Function

#include <math.h>
#include <stdio.h>
int main() {
    double base, exp, result;
    printf("Enter a base number: ");
    scanf("%lf", &base);
    printf("Enter an exponent: ");
    scanf("%lf", &exp);
    // calculates the power
    result = pow(base, exp);
    printf("%.1lf^%.1lf = %.2lf", base, exp, result);
    return 0;
}

The result of the above c program; as follows:

Enter a base number: 10
Enter an exponent: 2
10.0^2.0 = 100.00

C Program to Find the Power of a Number using For Loop

/**
 * C program to find power of any number using for loop
 */
#include <stdio.h>
int main()
{
    int base, exponent;
    long long power = 1;
    int i;
    /* Input base and exponent from user */
    printf("Enter base: ");
    scanf("%d", &base);
    printf("Enter exponent: ");
    scanf("%d", &exponent);
    /* Multiply base, exponent times*/
    for(i=1; i<=exponent; i++)
    {
        power = power * base;
    }
    printf("%d ^ %d = %lld", base, exponent, power);
    return 0;
}

The result of the above c program; as follows:

Enter base: 10
Enter exponent: 2
10 ^ 2 = 100

C Program to Find the Power of a Number using While Loop

#include <stdio.h>
 
int main()
{
  int i = 1, Number, Exponent; 
  long Power = 1;
  
  printf("\n Enter base Number : ");
  scanf(" %d", &Number);
  printf("\n Enter an Exponent Number: ");
  scanf(" %d", &Exponent);
    
  while(i <= Exponent)
  {
  	Power = Power * Number;
  	i++;
  }
  
  printf("\n The Final result of %d Power %d = %ld", Number, Exponent, Power);
  
  return 0;
}

The result of the above c program; as follows:

Enter base Number : 10
Enter an Exponent Number: 2
The Final result of 10 Power 2 = 100

More C Programming Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *