I want to display 1 to 100 without using loops. I already tried storing 1-100 in array but it also requires use of loops.
Asked
Active
Viewed 1.1k times
-5
-
2Show what you have tried. – Jens Feb 11 '15 at 07:18
-
3With all due respect. This makes absolutely no sense. You will need atleast one "conditional check" to see if the value is 100. – TheLostMind Feb 11 '15 at 07:19
-
1The key here is recursion (try it). But as TheLostMind said, you'll need at least one conditional check. – Paco Abato Feb 11 '15 at 07:20
-
can we do it without using loops. if so please tell me how – Pallav Feb 11 '15 at 07:20
-
7`System.out.println("1");System.out.println("2");System.out.println("3");...System.out.println("99");System.out.println("100");` – piet.t Feb 11 '15 at 07:20
-
@Pallav - You can do it using recursion as *Paco* says. But without *conditional* check. Nopes.. Note that using recursion is almost always *inefficient*. – TheLostMind Feb 11 '15 at 07:21
-
Maybe [printing from a file stream](http://stackoverflow.com/a/2950638/205233) will do the trick. – Filburt Feb 11 '15 at 07:21
-
2@Pallav You can write 100 `System.out.println()` statements – Jens Feb 11 '15 at 07:22
-
Use Clojure code from java with (range 1 100) in it. Same trick can be done with Scala etc. – boucekv Feb 11 '15 at 07:23
-
@Jens nice one .. :D but your way is too hard to follow :D – Pallav Feb 11 '15 at 07:26
-
will try recursion... thanks @Paco Abato – Pallav Feb 11 '15 at 07:27
-
@Pallav You can also do `System.out.println("1,2,3,4,5..."); ` – Jens Feb 11 '15 at 07:43
-
1Marked it as exactly [Duplicate of Display numbers from 1 to 100 without loops or conditions](http://stackoverflow.com/questions/2044033/display-numbers-from-1-to-100-without-loops-or-conditions) – Suhaib Janjua Feb 11 '15 at 07:53
6 Answers
1
Try this (Java 8)
IntStream.range(1, 100).forEach(n -> { System.out.println(n); });
However, implementation of range()
as well as forEach()
uses loops, so, the solution may be on the edge of cheating.
If you consider the code above as cheating, you can emulate loop, via, say, recursion:
private static void printIt(int n) {
System.out.println(n);
if (n < 100)
printIt(n + 1);
}
...
printIt(1);

Dmitry Bychenko
- 180,369
- 20
- 160
- 215
-
6
-
4But that `forEach`... I mean... Technically speaking... It is still a loop ;D – Matt Clark Feb 11 '15 at 07:24
-
Eh... Technically, `forEach` is not a *loop* by itself, its a *method* which, however, uses a loop... Actually, `range()` should use loop as well. So the solution is on the edge of cheating – Dmitry Bychenko Feb 11 '15 at 07:54
0
Use toString() method like this:
System.out.println(array.toString());
It converts array to this:
[1,2,3,4...]
http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object[])

Zlopez
- 660
- 1
- 5
- 11
-
-
-
-
-
if you will have it short you can use `System.out.println("1,2,3,4,5...");` – Jens Feb 11 '15 at 07:40
-
0
Try this,
import java.util.Arrays;
Code
int[] array = new int[] { 1, 2, 3, ... , 100 };
System.out.println(Arrays.toString(array));
Output:
[1, 2, 3, 4, ... , 100]

Suhaib Janjua
- 3,538
- 16
- 59
- 73
-
You have to declare an array 100 values if you want to print values without using any loop and condition. – Suhaib Janjua Feb 11 '15 at 07:55
0
You never said it had to print them only once or that program had to terminate successfully:
public static void main(String args[])
{
print(0);
}
private static void print(int i)
{
System.out.println((i % 100) + 1);
print(i+1);
}

Mike Zboray
- 39,828
- 3
- 90
- 122
0
public static void main(String[] args) {
print(100);
}
private static void print(int n) {
if(n > 1) {
print(n-1);
}
System.out.println(n);
}

hack4m
- 285
- 2
- 5
- 11
-1
Try Below code
public static void main(String[] args) {
int[] i = {1,10,50};
System.out.println(Arrays.toString(i));
}

Keval
- 1,857
- 16
- 26