Help with powers

This is a discussion on Help with powers within the C Programming forums, part of the General Programming Boards category; Can anyone tell me how to output code that shows the power of 2 all the way up to 2 ...

  1. #1
    Tim
    Guest

    Help 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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    for ( i = 0 ; i < 10 ; i++ ) pow( 2, i );

    The rest is pretty printing

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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 );
    Simple.

    -Prelude
    My best code is written with the delete key.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mathmatics Question?? Powers and inequalities.
    By xddxogm3 in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-26-2004, 11:12 PM
  2. On Women and Their Powers
    By UnregdRegd in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 03-30-2004, 04:55 PM
  3. Powers and simplifications, please help me
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-21-2003, 07:09 PM
  4. With super powers would you:
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 09-18-2003, 12:27 AM
  5. Square roots / Powers etc.
    By Robert602 in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2001, 04:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21