Display numbers from 1 to 100 without loops or conditions
java, loops
Solution
Pseudo code. Uses an array to force an exception after 100 elements which is caught and does nothing.
function r(array a, int index){
a[index] = a[index-1]+1
print a[index]
r(a, index+1)
}
try{
array a;
a.resize(101)
r(a, 1)
}catch(OutOfBoundsException){
}
EDIT Java code:
public void printTo100(){
int[] array = new int[101];
try{
printToArrayLimit(array, 1);
}catch(ArrayIndexOutOfBoundsException e){
}
}
public void printToArrayLimit(int[] array, int index){
array[index] = array[index-1]+1;
System.out.println(array[index]);
printToArrayLimit(array, index+1);
}
Problem
Is there a way to print numbers from 1 to 100 without using any loops or conditions like "if"? We can easily do using recursion but that again has an if condition. Is there a way to do without using "if" as well? Also no repetitive print statements, or a single print statement containing all the numbers from 1 to 100. A solution in Java is preferable.