java_logo

Java Print 1000 Hello World Without Loop

One of the Java questions I faced is to create a program which prints 1000 Hello World without using loop. So how do we solve this? The solution is to use recursion!

public class HelloWorld {

	public static void main(String[] args) {
		
		printThousandHelloWorld(1);
		
	}

	private static void printThousandHelloWorld(int n) {
		
		if(n < 1000) {
			printThousandHelloWorld(n+1);
		}
			System.out.println("Hello World");
		
	}
	
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.