Created
April 22, 2012 20:35
-
-
Save alexandream/2466718 to your computer and use it in GitHub Desktop.
Print 1 to 100 without loops or conditionals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RecursionException { | |
// Print and loop through recursion, throw exception when n reaches zero. | |
public static void loop(int n, int limit) { | |
int x = 1/n; | |
System.out.printf("%1$d\n", (limit - n) + 1); | |
loop(n - 1, limit); | |
} | |
public static void print(int n) { | |
loop(n, n); | |
} | |
public static void main(String[] args) { | |
try { | |
print(100); | |
} | |
catch (Exception e) { | |
/* do nothing, just to exit cleanly. */ | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment