Print "Hello world 1000 times" without using loop

bflack 0 Tallied Votes 4K Views Share

This snippet could be a problem solver for a tricky programing problem in entrance exams. Hoping that this has no buggs. haha. Didn't use compiler for this

main()
{
	int x=1;
	
	clrscr();
	repeater(x);
	getch();
}

repeater(int x)
{
	if(x<=1000)
	{
		printf("\nHello World");
		x++;
		repeater(x);
	}
}
TrustyTony 888 pyMod Team Colleague Featured Poster

clrscr is not standard c, this is just replacing normal loop with recursion, you can allways do that without too much difficulty, usually with increased memory use and running time. I can not see anything tricky here.How about not allowing recursion also and producing 1024 messages fixed. Maybe slightly more interesting.

skilly 23 Posting Whiz in Training

i have not touched a code in over ten years, but isn't the "if" class a loop? how are you NOT using a loop?

Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

isn't the "if" class a loop?

No, if() is a conditional statement (not class). It is a branching construct, not looping.

Do(), while() and for() are common looping constructs.

skilly 23 Posting Whiz in Training

aha...i see...semantics are important....but it still kinda acts like a loop when combined with the increment(in my opinion). you have redeemed yourself to me. next time i will vote you up(your help has equalized the downvote i received from you).

rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

Speaking of recursion, I once wrote a test for compiler exception handling (when exceptions were new to C++ in the early 90's) by implementing a Fibbonacci algorithm using exceptions only. You could use that methodology here as well... :-) Anyway, you would be surprised on how many compilers failed that test!

And I'd post the code here except for the fact that I'd have to dust off one of my old floppy disc systems since the code is in one of my old floppy disc archives...

TrustyTony 888 pyMod Team Colleague Featured Poster

How about not allowing recursion also and producing 1024 messages fixed.

#include <stdio.h>

#define d0(a) a
#define d1(a) d0(a) d0(a)
#define d2(a) d1(a) d1(a)
#define d3(a) d2(a) d2(a)
#define d4(a) d3(a) d3(a)
#define d5(a) d4(a) d4(a)
#define d6(a) d5(a) d5(a)
#define d7(a) d6(a) d6(a)
#define d8(a) d7(a) d7(a)
#define d9(a) d8(a) d8(a)
#define d10(a) d9(a) d9(a)

int main()
{
    printf(d10("Hello World!\n"));
    return 0;
}

Or the OP challenge from binary value of 1000:

#include <stdio.h>

#define d0(a) a
#define d1(a) d0(a) d0(a)
#define d2(a) d1(a) d1(a)
#define d3(a) d2(a) d2(a)
#define d4(a) d3(a) d3(a)
#define d5(a) d4(a) d4(a)
#define d6(a) d5(a) d5(a)
#define d7(a) d6(a) d6(a)
#define d8(a) d7(a) d7(a)
#define d9(a) d8(a) d8(a)
#define d10(a) d9(a) d9(a)
#define d1000(a) d9(a) d8(a) d7(a) d6(a) d5(a) d3(a) // binary 1111101000

int main()
{
    printf(d1000("Hello World!\n"));
    return 0;
}
Member Avatar for SoreComet
SoreComet

PS : I am aware that this post is 3 months old. Yet I find this interesting.
@pyTony You are the Great Programmer :)

This is really a great post. I thought that without recursion and looping, its NEVER possible to display anything repeatedly.

I changed the code and displayed the "Hello World!" with the current number of iteration i.e,

Hello World! : 1
Hello World! : 2
Hello World! : 3

and so on....but I wonder that its not possible to print such without using a loop or Recusive method. Is it possible? Please do post your opinions.

TrustyTony 888 pyMod Team Colleague Featured Poster

Until 1024 it is simple, I leave 1000 case excersise to you

#include <stdio.h>

#define d0(a, i) printf(a, i);
#define d1(a, i) d0(a, i-1) d0(a, i)
#define d2(a, i) d1(a, i-2) d1(a, i)
#define d3(a, i) d2(a, i-4) d2(a, i)
#define d4(a, i) d3(a, i-8) d3(a, i)
#define d5(a, i) d4(a, i-16) d4(a, i)
#define d6(a, i) d5(a, i-32) d5(a, i)
#define d7(a, i) d6(a, i-64) d6(a, i)
#define d8(a, i) d7(a, i-128) d7(a, i)
#define d9(a, i) d8(a, i-256) d8(a, i)
#define d10(a, i) d9(a, i-512) d9(a, i)

int main()
{
    d10("Hello World! %i\n", 1024);
    return 0;
}

@pyTony You are the Great Programmer :)

If you think so, I hope you voted one of my Python codes in the Code Snippet competition! ;)

Member Avatar for SoreComet
SoreComet

@ pyTony Man YOU ARE AWESOME :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.