Thursday, March 31, 2011

How to Print from 1 to 300000 without loop or Conditionals ?

In ILUG Kochi group , a question was posted by a member and I am giving my solution here


#include <stdio.h>


#include <stdlib.h>

#include <setjmp.h>



static jmp_buf buf;







int main( int argc , char **argv )

{

int i=0;





try {

setjmp(buf); 

printf("%d\n",i++);

longjmp(buf,i/(i%300001));

}

catch(...) {} 





}







2 comments:

Hari said...

I got another solution. Not really elegant since it depends on the OS signal. Still!!

#include "stdafx.h"

int nTotCnt = 300000;

int main (int argc, char* argv[])
{
unsigned int nCnt = 1, k = 0;

again:
printf ("%d\n", nCnt);
k = nCnt++/--nTotCnt;
goto again;

return 0;
}

Sujith K.S said...

Mixing C++ exception handling and C-style exception handling. Nice one, Praseedji