Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class RangeWithStepPrinter {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- System.out.println("Type the number from which to start ");
- int from = in.nextInt();
- System.out.println("Type which number to finish ");
- int last = in.nextInt();
- System.out.println("Type step ");
- int step = in.nextInt();
- int n;
- if (from < last) {
- for (n = from; n <= last; n += step) {
- System.out.print(n + " ");
- if (step < 0) {
- throw new IllegalArgumentException();
- }
- }
- } else {
- for (n = from; n >= last; n += step) {
- System.out.print(n + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement