Skip to content

Instantly share code, notes, and snippets.

@alexandream
Created April 22, 2012 20:35
Show Gist options
  • Save alexandream/2466718 to your computer and use it in GitHub Desktop.
Save alexandream/2466718 to your computer and use it in GitHub Desktop.
Print 1 to 100 without loops or conditionals
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