Can anyone tell me how to output code that shows the power of 2 all the way up to 2 to the 10th power and have it show on one line? i know that the code for power is pow(x,y) but don't know exactly how to use it. Thanks

Printable View

- 05-02-2002TimHelp with powers
Can anyone tell me how to output code that shows the power of 2 all the way up to 2 to the 10th power and have it show on one line? i know that the code for power is pow(x,y) but don't know exactly how to use it. Thanks

- 05-02-2002Salem
for ( i = 0 ; i < 10 ; i++ ) pow( 2, i );

The rest is pretty printing - 05-02-2002Prelude
You could just not use the pow function. Since you're doing the powers of two you can shift bits around with the same result.

Code:`unsigned x, n;`

for ( x = 0, n = 1; x < 10; x++, n <<= 1 )

printf ( "%u ", n );

-Prelude - 05-03-2002Hammer
Found this one in my book.... it might help someone ;)

Code:`#include <stdio.h>`

#define TWO_TO_THE(x) (1L <<(x))

#define FOUR_TO_THE(x) (1L <<((x) <<1))

int main(void)

{

printf("%ld\n", FOUR_TO_THE(3));

printf("%ld\n", TWO_TO_THE(10));

return (0);

}