Print numbers in specific range without using any loop or conditions (Java)

Oghli :

Maybe first idea come to mind to solve this kind of problems is recursive function but also it will be a challenge to write recursive function without any condition.

I tried this approach to print numbers from 10 to 60:

public static void printNumbers(int n){
       int divisonByZero = 1 / (61 - n);
       System.out.println(n);
       printNumbers(n+1);
}     
public static void main(String[] args) {
       printNumbers(10);
}   

But it will crash when it reach number 61 without exception handling

also even with try catch the Arithmetic Exception it's still not a preferable solution because it's handling an exception (runtime error).

I think main problem when using recursive functions is the stop condition.

As well I read that there is a way in C++ by creating a class with a static variable counter and initialize it then increment the counter variable and print it in the constructor after that instantiating number of objects of class counter will print these numbers.

Any suggested solutions to solve this challenge would be appreciated.

ToYonos :

Based on @Imposter's answer, a reduce version with compacted but readable code

class Sandbox
{
    public static void main(String args[]) throws Exception
    {
        System.out.println(getfalse(10, 60));
    }

    public static String getfalse(Integer start, Integer stop) throws Exception
    {
        return
            start + "\n" +
            Sandbox.class.getMethod("get" + (start == stop), Integer.class, Integer.class)
            .invoke(Sandbox.class, start+1, stop);
    }

    public static String gettrue(Integer start, Integer stop)
    {
        return "";
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=463266&siteId=1