Java Arena
Step 3 of 7

Bounds from input

Both ends of a loop can come from the input. The loop then runs as many rounds as the numbers say:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int from = Integer.valueOf(scanner.nextLine());
        int to = Integer.valueOf(scanner.nextLine());
        for (int i = from; i <= to; i++) {
            System.out.println(i);
        }
        System.out.println("End");
    }
}
Input
-2
1
Output
-2
-1
0
1
End

What if the start is bigger than the end, say from 5 to 3? The condition 5 <= 3 is false before the first round, so the loop's block doesn't run at all, and the program goes straight on to End. A loop that runs zero times is normal, and a program should still print something sensible when it happens.

Your task

Challenge 1 of 3

Read a start and an end, and print every number from the start to the end. For the input 3 and 6 the program prints:

3
4
5
6

Challenge 2

Read a start and an end. Print every number from the start to the end, and then how many numbers that was, as N numbers. When the start is bigger than the end, only 0 numbers is printed. For the input 4 and 6 the program prints:

4
5
6
3 numbers

Challenge 3

Read a start and an end, and print every second number from the start, as long as it's at most the end. For the input 1 and 9 the program prints:

1
3
5
7
9