Program to find Power of Number in C Language

Program-to-find-Power-of-Number-in-C-Language

Program Introduction:

Write a program to calculate the Power of Number in C programming. The program should accept two numbers from the user, one is base and another is exponent and calculate the power.

What are the Base, Exponent, and Power of a Number?

The power is equal to the repeated multiplication of the base, exponent times.

If the base is 2 and exponent is 5. Then power is equal to 2 multiplied 5 times – 2 * 2 * 2 * 2 * 2, which (Power of number) is 32.

We can write the Power of Number program manually by using the C Loops like While, for loops. and we also have a built-in C function called pow which is used to calculate the power of the number. So we can simply use the pow function to calculate the power. Let’s look into these two cases.

Excepted Inputs and Outputs:

Let’s quickly look at the expected inputs and outputs of the program

Input:

Output:

Program Pre-Requisites:

It is recommended to have the basic knowledge of the C Loops like while loop and for loop and goto statement. As we are going to use them in this program.

As specified earlier, We are going to look at different methods to calculate the power of number. Let’s start.

Method 1: Calculate the power of Number in C language using loops Algorithm:

  1. Take the base and exponent values from the user and store them in variables base and exponent
  2. The base and exponent should be positive numbers, So check if the user provided negative numbers for any of them ( base or exponent values). If a negative number is provided, Display the error message Invalid Input, Please try again and stop the program.
  3. If both base and exponent are positive, We are going to continue to the next step.
  4. The Power is the repeated multiplication of the base, exponent times. So we use the loop to multiply the base, exponent times.
  5. We use a res variables initialized to 1.0 and shall be used to maintain the power so far.
  6. At Each iteration
    • We multiply the base with res and add it to res. res = res * base;
    • Then we decrement the exponent by one. exponent--;
    • This loop continues until the exponent became zero ( exponent > 0 )
  7. Once the loop is terminated, The res variable contains the result (Power of number).

Method 1.1: Calculate the power of Number in C language using While Loop:

Here is the power of the number program using the while loop.

We have used the doubledatatype for base variable and integer datatype for the exponent variable(for simplicity).

The res (i.e power) is a long doublevariable to hold large values.

Program Output:

Compile and run the program.

As you can see we are getting the expected results.

Let’s try double value for base variable

What happens if the user provides Negative numbers ( if base is negative or exponent is negative or both are negative)

The program is displaying an error notice if the user enters the negative number for neither of base or exponent variables.

power-of-number-program-output

Method 1.2: Calculate the power of Number in C language using for Loop:

Let’s rewrite the above program to use the for loop instead of the whileloop.

We can take advantage of the for loops, Initialization, and update steps.

  • So we can Initialize the res variable at for loop’s Init step – i.e res = 1.0;
  • And we can also update the exponent value at for loops update step. – i.e exponent--

So our for loop looks something like the below.

for(res = 1.0; exponent > 0; exponent – -)

Here is the full program

Program Output:

Let’s compile and run the program and try different test cases

Method 2: Calculate the Power of Number in C using pow function:

We can use the inbuilt pow function from the math.h library to calculate the power of a number in C.

Here is the syntax of the pow function.

double pow(double x, double y)

📢 Don’t forget to add the math.h header file to the program.

Let’s use the in-built Pow function to calculate the power of the number.

Program Output:

We need to add the -lm option to program during the compilation to avoid the linking error.

As the pow function is part of the math library, We need to add the -lm flag. Here is how we compile the program in Linux

$ gcc power-of-number.c  -lm

The above command generates the a.out executable file. Let’s run it and test few test cases.

As you can see we are getting the expected results.

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

3 Responses

  1. […] C Program to Calculate the Power of Number […]

  2. […] of rem with nDigits(Number of digits in n) – i.e power(rem, nDigits);. We are using the Power of Number Program in C to calculate the power instead of the pow built-in function. Then Update the arm […]

  3. […] C Program to Calculate the Power of Number […]

Leave a Reply