## Wednesday, August 15, 2007

### Write a program to print numbers from 1 to 100 without using loops!

Another "Yeah, I am a jerk, kick me! kind of a question. I simply dont know why they ask these questions.

Nevertheless, for the benefit of mankind...

Method1 (Using recursion)

void printUp(int startNumber, int endNumber)
{
if (startNumber > endNumber)
return;

printf("[%d]\n", startNumber++);
printUp(startNumber, endNumber);
}

Method2 (Using goto)

void printUp(int startNumber, int endNumber)
{
start:

if (startNumber > endNumber)
{
goto end;
}
else
{
printf("[%d]\n", startNumber++);
goto start;
}

end:
return;
}

Jayesh said...

class MyClass
{
public:
MyClass() { printf("%d\n",count++); }
private:
static int count;
};

int MyClass::count = 1;

int main()
{
MyClass myArray[100];
return 0;
}

Sandeep Patra said...

here is a smaller code to print 1 to N using recursion.

Input Parameter = N

void print_1_n_without_loop (int n) {
if(n) {
print_1_n_without_loop(n-1);
printf(" %d -> ",n);
}
}

Anonymous said...

sandeep, if you can ensure that the stack still has enough space to store the return addresses, then it'll work. in reality, it's bad practice doing any recursion that can't be optimized to use a constant stack space.

this would look bad to anyone that knows how recursion actually works(that includes many professionals). how the author implemented the tail recursion is standard practice.

Anonymous said...

on a funny note ,
to print 100 number without using loops;

printf("1");
printf("2");
printf("3");
printf("4");
printf("5");
printf("6");
printf("7");
printf("8");
printf("9");
...
...
...
...

printf("100");

krun said...

Hi everyone i just need a program in c with following things::

-----------------------------------

i just want the numbers to be printed in cyclical order i.e if i input the number as 4 then the output should be 4*4=16 , i.e from 1-16 the numbers should be in cyclical order

see this...
from (1-4)

first it will print ... 1 2 3 4
then it will print 5 (5-7)
6 i.e (n-1)+n
10 9 8 7 i.e it should print 4+3 in "Y" axis (downwards)
now here it will print n+3+3 in "X" axis (i.e 4+3+3)=10
i.e from (8-10) in "X" axis in (left direction)

and now form 11 to 12 it will print in "Y" axis (upwards)

i.e-- 1 2 3 4
12 5
11 6 here it will print 11-12 in "Y" axis but in (upward)
10 9 8 7 direction ......

then after it will print 13-14 in "X" axis (rightwards)

then after that it will print 15 in "Y" axis (downward)
then after that it will print 16 in "X' axis (leftward)

the final output will be

1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7

Sangamesh said...

Can any one help me to get a C/C++ program to add a user to MySQL. The user should be permitted to only "INSERT" into the given database.

Anonymous said...

We can do this by using non local jumps setjmp() and longjmp() ..but it becomes inefficient...

anomyous said...

some one plz write a c++ code to print 1 to 100 prime nos without loops(for,while,if..).plz dnt use 100 cout statements

sandeep kumar said...

Thanks for the info. It is very helpful.
RegardsEducational site
Get jobs info at Educational site