Java – How to print name 1000 times without looping?

Here’s a Java example to print a name 1000 times without looping or recursion, instead, use String concatenation and simple math. Just for fun.

JavaNoLoop.java

package com.mkyong.samples;

public class JavaNoLoop {

    public static void main(String[] args) {

        String s1 = "Mkyong\n";
        String s3 = s1 + s1 + s1;
        String s10 = s3 + s3 + s3 + s1;
        String s30 = s10 + s10 + s10;
        String s100 = s30 + s30 + s30 + s10;
        String s300 = s100 + s100 + s100;
        String s1000 = s300 + s300 + s300 + s100;
        System.out.print(s1000);

    }

}

Output


Mkyong
Mkyong
Mkyong
... 1000 times

References

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments