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?
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?
With a for loop ?
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction
Write pow() yourself.
I don't think you want to do that. Just use pow() if you can.
yeah, i have to do it without using pow(x,y)
Use a for loop. Or use a for loop that squares and loops through the bits of y.
There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.
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("%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; }
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.Code:int power(int base, int ext) { int i; int res=1; for(i=1; i <= ext; i++) res = res * base; return res; }
ssharish2005
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.
Last edited by zacs7; 08-30-2007 at 08:16 PM. Reason: Giving the answer
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.
Oh, you mean it only has to work for integer values of y?
Well I wont bother to post my solution then.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"