C program to Find the Power of a Number

How to calculate the power of a number in C programming?

A power of a number tells us how many times the number needs to be multiplied with itself. There are various ways to write a C program to Find the Power of a Number, using loops or pre defined functions in C. For calculating you need to have the base and exponent.

C-program-to-find-the-power-of-a-number-image

Syntax:

General code to find the power of a number in C.
while (exp != 0) 
{
        result *= base;
        --exp;
}

Syntax:

General code to find the power of a number in C using pre-defined function in “math.h” library .
double pow(double num1, double num2);
  • num1 – it is the base value.
  • num2 – it is the exponential or power value.

Steps to calculate the power of a number number:

  • Step 1: Start
  • Step 2: Declare the variable num, num2, res.
  • Step 3: Read input num1 and num2 from the user or predefine it according to the need.
  • Step 4: Use the appropriate syntax that you need.
  • Step5: Display the result in the stdout console.
  • Step 6: Program end.

Working:

In these program we will learn to do the product of two number using various different ways.

Example 1 :

A simple program where we will find the power of a number that is predefined.

Run
#include<stdio.h>
int main()
{
    int num1=5, num2=5;
    long double result = 1.0;
    while (num2 != 0)
    {
        result *= num1;
        --num2;
    }
    printf("Answer = %.0Lf", result);
    return 0;
}

Output:

Answer = 3125

Example 2 :

Program where we will calculate the power of a numbers given by the users.

Run
#include<stdio.h>
int main()
{
    int num1, num2;
    long double result = 1.0;
    printf("Enter a base number: ");
    scanf("%d", &num1);
    printf("Enter an exponent: ");
    scanf("%d", &num2);
    while (num2 != 0)
    {
        result *= num1;
        --num2;
    }
    printf("Answer = %.0Lf", result);
    return 0;
}

Input:

Enter a base number: 5
Enter an exponent: 4

Output:

Answer = 625

Example 3 :

Program where we will find the power of a number in C language using pre-functions available in “math.h” library.

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

Input:

Enter a base number: 4
Enter an exponent: 6

Output:

4.0^6.0 = 4096.00

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription