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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar