# Can't raise x to the power of y

This is a discussion on Can't raise x to the power of y within the C Programming forums, part of the General Programming Boards category; Hi, is there a way to raise x to the power of y (x^y), without using the math.h library and ...

1. ## Can't raise x to the power of y

Hi, is there a way to raise x to the power of y (x^y), without using the math.h library and the pow(x,y) function?

2. With a for loop ?

3. Write pow() yourself.

I don't think you want to do that. Just use pow() if you can.

4. yeah, i have to do it without using pow(x,y)

5. So write your own pow() function.

6. Use a for loop. Or use a for loop that squares and loops through the bits of y.

7. Hi I'm trying to use the for loop, but i'm having trouble figuring out how to use it. Do you know what i'm doing wrong?

Code:
```  int n;
int output[60];
int index = 0;
int condition;
int quotient;
int size = 0;
int nsize;
int converted_number=0;
int power;
int i;

printf("Enter base(b) between [1,10], and a number(n), such that digits of n is between[0,b-1] in this format 'n b': ");
scanf("&#37;d %d", &n, &b);

printf("n is: %d\n", n);   //Test
printf("b is: %d\n", b);   //TEST
nsize = n;

if ((b < 1) || (b > 10))
{
printf("Your base is not between 1 and 10");
return 0;
}

if (n < 0)
{
printf("n must be positive");
return 0;
}

while (nsize != 0)  //counting the total number of digits
{
nsize = nsize/10;
++size;
}
printf("size of n is: %d \n", size);  //testing to see the size

while (size > 0)
{
output[index] = n % 10;
n = n/10;
printf("number to convert: %d\n", output[index]);

for (i = 1; i <=index; i++)       //FOR LOOP IN HERE!!!
{
power = output[index]*;      //NOT SURE ON WHAT TO DO HERE
converted_number += (power*output[index]);
}

printf("%d * %d^%d %d\n", output[index], b, index, converted_number);
--size;
index++;
}

printf("converted number is: %d", converted_number);

return 0;
}```

8. Code:
```int power(int base, int ext)
{
int i;
int res=1;

for(i=1; i <= ext; i++)
res = res * base;

return res;
}```
here is a basic pow function. It can handle just int. You will have to work around to make it to work for all datatype.

ssharish2005

9. x^y simply means, mutliply x by itself y times.

Such that, 5^10
= 5 * 5 * 5 * 5 *5 * 5 * 5 * 5 * 5 *5

Quite easy to put in a loop.

10. This sounds like a homework problem, but I'll give you the hint that pow(x, y) can be rewritten in terms of other functions from <math.h>, so that's one way, if you're allowed to use those. Otherwise, if y is an integer, you can implement it in terms of a loop as described above.

11. Oh, you mean it only has to work for integer values of y?
Well I wont bother to post my solution then.