Try this:

long int x_to_the_n (int x,int n)
{
    int i; /* Variable used in loop counter */
    int number = 1;

    for (i = 0; i < n; ++i)
        number *= x;

    return(number);
}
Answer from niculare on Stack Overflow
🌐
Reddit
reddit.com › r/c_programming › is there any way to find the power of a number without using pow() function or looping statements in c-language ?
r/C_Programming on Reddit: Is there any way to find the power of a number without using pow() function or looping statements in C-language ?
May 29, 2021 - A subreddit for all questions related to programming in any language. ... c program to find whether the number is a power of 3 or not, in the output I keep getting it is not a power of 3
🌐
Sololearn
sololearn.com › en › Discuss › 3077653 › how-i-can-write-power-base-code-without-using-pow-function-in-c
How i can write power base code without using pow () function in c | Sololearn: Learn to code for FREE!
//complete answer about this code at https://code.sololearn.com/c24WohfoM7aB/?ref=app #include <stdio.h> double power(double num,int p) { double ret=num; if(!p)return 1; if(p<0){ret=num=1/num;p=-p;} while(--p) ret*=num; return ret; } int main() { int num,p; printf("Enter number & power:\n"); scanf("%d %d",&num,&p); printf("pow(%d,%d) = %.15f", num, p, power(num,p)); } 26th Aug 2022, 7:58 PM ·
🌐
Medium
medium.com › @maxnegi333 › how-to-calculate-the-power-of-an-integer-without-using-pow-in-c-6f3d21b459d2
How to calculate the power of an integer without using POW() in C? - Mayanknegi - Medium
June 15, 2023 - How to calculate the power of an integer without using POW() in C? To get the power of a given number, use the pow() function. In this post, we’ll learn how to determine an integer’s power …
🌐
Edureka Community
edureka.co › home › community › categories › c++ › how could i use the power function in c c ...
how could I use the power function in c c without pow functions or recursion | Edureka Community
June 20, 2022 - I'm using a C++ compiler, but I'm writing C code (if that helps) There is a string of numbers. (-1^(a-1)/2a-1 ... * i - 1) * (pow(x, 5 * i - 1)); }
🌐
Stack Overflow
stackoverflow.com › questions › 71530491 › how-to-calculate-to-the-power-of-n-in-c-without-using-pow-or-anything-from-the-m
for loop - How to calculate to the power of n in c without using pow or anything from the math.h library - Stack Overflow
The task goes like this: Write a program that calculates the power of a number (as a pow function from the "math.h" library), except the limitation here is that the exponent can only be an
🌐
YouTube
youtube.com › education motivation and entertainment
How to calculate the power without using POW function in C Language - YouTube
Program to calculate the power without using POW function in C programming language.Please subscribe to my channel for more questions related to Any Program...
Published   September 3, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › c language › power-function-c-cpp
Power Function in C/C++ [pow()] - GeeksforGeeks
April 30, 2025 - One such example is pow(5, 2). When assigned to an integer, it outputs 24 on some compilers and works fine for some other compilers. But pow(5, 2) without any assignment to an integer outputs 25.
Find elsewhere
🌐
Quescol
quescol.com › home › c program to calculate the power without using pow function
C Program to calculate the power without using POW function - Quescol
May 7, 2025 - Our program will take two integers as an input. One is base and another is power. Now to calcualte the power without using any existing library method, we will multiple base number to itself for power times.
Top answer
1 of 2
2

You can use macros to get away with no function calls restriction as macros will generate inline code which is technically not a function call

however in case of more complex operations macro can not have return value so you need to use some local variable for the result (in case of more than single expression) like:

int ret;
#define my_pow_notemp(a,b) (b==0)?1:(b==1)?a:(b==2)?a*a:(b==3)?a*a*a:0
#define my_pow(a,b)\
    {\
    ret=1;\
    if (int(b& 1)) ret*=a;\
    if (int(b& 2)) ret*=a*a;\
    if (int(b& 4)) ret*=a*a*a*a;\
    if (int(b& 8)) ret*=a*a*a*a*a*a*a*a;\
    if (int(b&16)) ret*=a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a;\
    if (int(b&32)) ret*=a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a;\
    }
void main()
   {
   int a=2,b=3,c;
   c=my_pow_notemp(a,b); // c = a^b
   my_pow(a,b); c = ret; // c = a^b
   }

as you can see you can use my_pow_notemp directly but the code is hardcoded so only up to a^3 if you want more you have to add it to code. The my_pow is accepting exponents up to a^63 and its also an example on how to return value in case of more complex code inside macro. Here are some (normal) ways on how to compute powers in case you need non integer or negative exponents (but to convert it to unrolled code will be insanely hard without loops/recursion):

  • Power by squaring for negative exponents

In case you want to get away with recursion and function calls you can use templates instead of macros but that is limited to C++.

template<class T> T my_pow(T a,T b)
    {
    if (b==0) return 1;
    if (b==1) return a;
    return a*my_pow(a,b-1);
    }
void main()
   {
   int a=2,b=3,c;
   c=my_pow(a,b);
   }

As you can see templates have return value so no problem even with more complex code (more than single expression).

To avoid loops you can use LUT tables

int my_pow[4][4]=
    {
    {1,0,0,0},  // 0^
    {1,1,1,1},  // 1^
    {1,2,4,8},  // 2^
    {1,3,9,27}, // 3^
    };
void main()
   {
   int a=2,b=3,c;
   c=my_pow[a][b];
   }

If you have access to FPU or advanced math assembly you can use that as asm instruction is not a function call. FPU usually have log,exp,pow functions natively. This however limits the code to specific instruction set !!!

Here some examples:

  • How to: pow(real, real) in x86

So when I consider your limitation I think the best way is:

#define my_pow(a,b) (b==0)?1:(b==1)?a:(b==2)?a*a:(b==3)?a*a*a:0
void main()
   {
   int a=2,b=3,c;
   c=my_pow(a,b); // c = a^b
   }

Which will work on int exponents b up to 3 (if you want more just add (b==4)?a*a*a*a: ... :0) and both int and float bases a. If you need much bigger exponent use the complicated version with local temp variable for returning result.

[Edit1] ultimative single expression macro with power by squaring up to a^15

#define my_pow(a,b) (1* (int(b&1))?a:1* (int(b&2))?a*a:1* (int(b&4))?a*a*a*a:1* (int(b&8))?a*a*a*a*a*a*a*a:1)
void main()
   {
   int a=2,b=3,c;
   c=my_pow(a,b); // c = a^b
   }

In case you want more than a^15 just add sub term (int(b&16))?a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a:1 and so on for each bit of exponent.

2 of 2
1

It is a series. Replace pow() based on the previous iteration. @Bathsheba

Code does not need to call pow(). It can form pow(x, 5 * i - 1) and pow(-1, i - 1), since both have an int exponent based on the iterator i, from the prior loop iteration.

Example:
Let f(x, i) = pow(x, 5 * i - 1)
Then f(x, 1) = x*x*x*x
and f(x, i > 1) = f(x, i-1) * x*x*x*x*x

double power_n1 = 1.0; 
double power_x5 = x*x*x*x; 
for (int i = 1; i < j + 1; i++) 
  // sum += (pow(-1, i - 1)) / (5 * i - 1) * (pow(x, 5 * i - 1));
  sum += power_n1 / (5 * i - 1) * power_x5;
  power_n1 = -power_n1;
  power_x5 *= x*x*x*x*x; 
}
🌐
Shiksha
shiksha.com › home › it & software › it & software articles › programming articles › power function in c
Power Function in C - Shiksha Online
September 3, 2024 - You can calculate the power of a number in C without using the pow() function by implementing your own power function using loops or recursion.
🌐
Cplusplus
cplusplus.com › forum › general › 36492
exponential function without using pow() - C++ Forum
I am trying to write a program that solves exponents without using the pow () function. I think I need to use a loop to generate how many times the base multiplies itself as long as the exponent is positive. Any ideas? Thanks ... cppmatt Thanks for getting back to me! Your code works great, however if you don't mind, could you explain why you set T=1 & how T=T*n works? Sorry I'm just getting started in ...
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › write-you-own-power-without-using-multiplication-and-division
Write you own Power without using multiplication(*) and division(/) operators - GeeksforGeeks
#include<stdio.h> /* Works only if a >= 0 and b >= 0 */ int pow(int a, int b) { //base case : anything raised to the power 0 is 1 if (b == 0) return 1; int answer = a; int increment = a; int i, j; for(i = 1; i < b; i++) { for(j = 1; j < a; j++) { answer += increment; } increment = answer; } return answer; } /* driver program to test above function */ int main() { printf("\n %d", pow(5, 3)); getchar(); return 0; } Java ·
Published   July 23, 2025
🌐
Sololearn
sololearn.com › en › Discuss › 419549 › how-can-i-code-a-formula-with-power-without-using-pow
How can I code a formula with power without using pow | Sololearn: Learn to code for FREE!
Using numbers that are positives (Example: 2^2 = 4; (-3)^2= 9) and negatives (Ex: 2^-2 = 0.25; (-3)^-2=) ands obtaining complete results (decimals if they have) and also
Top answer
1 of 2
5

To calculate 2N in C, use 1 << N.

If this may exceed the value representable in an int, use (Type) 1 << N, where Type is the integer type you want to use, such as unsigned long or uint64_t.

<< is the left-shift operator. It moves bits “left” in the bits that represent a number. Since numbers are represented in binary, moving bits left increases the powers of 2 they represent. Thus, 12 represents 1, 102 represents 2, 1002 represents 4, and so on, so 1 shifted left N positions represents 2N.

2 of 2
0

Numbers can be represented in binary form. For example, if integers are stored using 32 bits, 1 is stored like this:

00000000 00000000 00000000 00000001

And the value is the result of 1 x (20)

If you do a left-shift operation your value will be stored as this:

00000000 00000000 00000000 00000010

That means that now the result is result of 1 x (21)

Bit used to store a type is sizeof(type)x8, because a byte is 8 bit.

So best method is to use shift:

The left-shift of 1 by exp is equivalent to 2 raised to exp. Shift operators must not be used for negative exponents in case of pow. The result is an undefined behaviour. Another case of undefined behavior is the one of shifting the number equal to or more than N, in case of that number is stored in N bits.

#include <stdio.h>
#include <stdlib.h>

int main() {
    int exp;
    printf("Please, insert exponent:\n");
    if (scanf("%d", &exp) != 1) {
        printf("ERROR: scanf\n");
        exit(EXIT_FAILURE);
    }
    if (exp < 0) {
        printf("ERROR: exponent must be >= 0\n");
        exit(EXIT_FAILURE);
    }
    printf("2^(%d) = %d\n", exp, 1 << exp);
    exit(EXIT_SUCCESS);
}

You can also do it creating a ricorsive function (int) -> int:

int my_pow(int exp) {
    If (exp < 0 ) {
        return -1;
    }
    if (exp == 0) {
        return 1;
    }
    if (exp > 0) {
        return 2 * my_pow(exp-1);
    }
}

Using it as main:

int main() {    
    int exp;
    scanf("%d" &exp);
    int res = my_pow(exp);
        if (res == -1) {
            printf("ERROR: Exponent must be equal or bigger than 0\n");
            exit(EXIT_FAILURE);
        } 
        printf("2^(%d) = %d", exp, res);
    return 0;
}
🌐
Programiz
programiz.com › c-programming › examples › power-number
C Program to Calculate the Power of a Number
And, the power is equal to 2*2*2 ... 0; } Output · Enter a base number: 3 Enter an exponent: 4 Answer = 81 · We can also use the pow() function to calculate the power of a number....