Programing Puzzles (spoilers)

BCS ao at pathlink.com
Wed Aug 6 15:06:49 PDT 2008


Reply to wyverex,

> just some fun little programming puzzles I found around online...
> 
> Problem #2 Test if an int is even or odd without looping or if
> statement (Cant use: do, while, for, foreach, if).

bool isEven(int i) {return !(i & 0x01);}

> 
> Problem #3 Write a program without using any loop (if, for, while etc)
> to print numbers from 1 to 100 and 100 to 1;

void DoIt(int i = 1)
{
    writef("%d\n", i);
    i==100 || DoIt(i+1);
    writef("%d\n", i);
}

> 
> Problem #4 Find if the given number is a power of 2.
> 

bool isPow(int i)
{
  if(!i) return true;
  while(!(i & 0x01)) i>>=1;
  return !(i ^ 0x01);
}




More information about the Digitalmars-d-learn mailing list