In this article, we will write a C program to find the power of a number without using the `pow()`

function. The program takes the number and exponent from the user as input, calculates its power and prints the result on the output window.

**Sample Example:**

Enter base and power: 5 3 5^3 = 125 Enter base and power: 10 0 10^0 = 1

In C programming, we generally use the built-in `pow()`

function to calculate the power of a number.

But In this program, we don’t want to use any built-in function. Instead, we will build our own logic to calculate the power of a given number.

The power of a number refers to the exponent to which the number is raised. In programming languages, it is generally denoted by the “^” or “**” symbol.

For example, 5^3 represents 5 raised to the power 3 and it is calculated as:

`5^3 = 5*5*5 = 125`

So, in general, if we want to calculate `a`

raised to the power `b`

, we can write it as:

`a^b = a*a*a.....b times`

If you pay enough attention to the above expression, you will find out that it represents a loop in the context of programming languages. Which means, that to calculate `a`

raised to the power `b`

, we can multiply `a`

by itself `b`

times which can be achieved using a `for/while`

loop.

Let’s use a simple `for`

loop to calculate the power of a given number:

// C program to calculate the power of a number // without using the pow() function #include <stdio.h> #include <conio.h> int main() { int num, pow, result = 1; printf("Enter base and power: "); scanf("%d %d" , &num, &pow); // Calculate num^pow for(int i = 0; i < pow; i++){ result = result * num; } // Print the result printf("%d^%d = %d", num, pow, result); return 0; }

**Output:**

Enter base and power: 10 3 10^3 = 1000

In this example, the for loop runs `pow`

times to calculate the power of the number.

## 2. Using a While Loop to Calculate the Power of a Number

We can also use a `while`

loop to calculate the power of a number without using the built-in `pow()`

function.

In this approach, we will copy the value of the `pow`

into a temporary variable `count`

and run the `while`

loop until `count`

becomes zero. Don’t forget to reduce `count`

by -1 on each iteration of the `while`

loop to avoid an infinite loop.

// C program to calculate the power of a number // without using the pow() function #include <stdio.h> #include <conio.h> int main() { int num, pow, result = 1, count; printf("Enter base and power: "); scanf("%d %d" , &num, &pow); // Copy the value of pow into count variable count = pow; // Calculate num^pow while(count != 0){ result = result * num; count--; } // Print the result printf("%d^%d = %d", num, pow, result); return 0; }

**Output:**

Enter base and power: 6 3 6^3 = 216

## 3. Using Recursion to Calculate the Power of a Number

In programming, recursion is a concept where a function calls itself again and again until a base case is reached. In each function call, the problem is divided into a smaller problem and it continues until the base condition is reached.

In calculating the power of a number, the base case is the power 0. This is because, if any number is raised to the power of 0, its result is always 1.

So, we will create a function `power()`

which takes the base number and power as its arguments and in each function call we will reduce the power by 1 until it becomes 0.

As soon as the power becomes 0, the function will stop calling itself and start backtracking to calculate the result.

See implementation in the following program:

// C program to calculate the power of a number // without using the pow() function #include <stdio.h> #include <conio.h> // Recursive function int power(int num, int exp){ // Base case if(exp == 0){ return 1; } return num*power(num, exp - 1); } int main() { int num, exp, result; printf("Enter base and power: "); scanf("%d %d" , &num, &exp); // Calculate num^exp result = power(num, exp); // Print the result printf("%d^%d = %d", num, exp, result); return 0; }

**Output:**

Enter base and power: 5 4 5^4 = 625

In this program, the recursive function `power()`

will continue calling itself until exp becomes zero. As soon as the value of exp becomes 0, it stops calling itself, and backtracking starts.

I hope you will find this article helpful. Thanks for reading.