Print numbers 1-n without using loop
- We can print 1....n numbers using recursive function in java like below :
public class printNumbersWithoutUsingLoop {
public static void main(String[] args) {
int n = 1, range = 10;
m1(n, range);
}
public static void m1(int i, int range) {
if (i <= range) {
System.out.print(i++ + " ");
m1(i, range);
}
}
}
Output:
|
print numbers 1-n without using loop |
- We can print 1....n numbers by using java streams like below :
import java.util.stream.IntStream;
public class printNumbersWithoutUsingLoops {
static int i = 0;
public static void main(String[] args) {
IntStream s = IntStream.iterate(1, i -> i + 1).limit(10);
s.forEach(System.out::print);
System.out.println("\n<<--------------->>");
IntStream s1 = IntStream.range(1, 11);
s1.forEach(System.out::print);
IntStream.range(1, 12).toArray();
}
}
Output:
|
print numbers 1-n without using loop |
Please comment below to feedback or ask questions.
No comments:
Post a Comment
Please comment below to feedback or ask questions.