C program to find power of a number using for loop

Write a C program to find power of a number using for loop. How to find power of a number without using built in library functions in C program. Logic to find power of any number without using pow() function in C programming.



Input base: 2
Input exponent: 5


2 ^ 5 = 32

Required knowledge

Basic C programming, For loop

Logic to find power of any number

In previous post I already explained to find power of a number using pow() function. Below is the step by step descriptive logic.

  1. Input base and exponents from user. Store it in two variables say base and expo.
  2. Declare and initialize another variable to store power say power = 1.
  3. Run a loop from 1 to expo, increment loop counter by 1 in each iteration. The loop structure must look similar to for(i=1; i<=expo; i++).
  4. For each iteration inside loop multiply power with num i.e. power = power * num.
  5. Finally after loop you are left with power in power variable.

Program to find power of any number

 * 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;

Note: Some compilers do not support long long data type hence if your compiler report errors in above program, then change data type from long long with long type also replace the format specifier %lld to %ld.

Take a step forward and learn this program using other recursive approach.

Read more - Program to find power using recursion.


Enter base: 2
Enter exponent: 5
2 ^ 5 = 32

Happy coding 😉

About Pankaj

Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write programming articles especially for beginners. He works at Vasudhaika Software Sols as a Software Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and Music lover.

Follow on: Facebook | Twitter | Google | or

Comments and discussion
Have a doubt, write here. I will help my best.
Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>