Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class RangeWithStepPrinter {
  4. public static void main(String[] args) {
  5. Scanner in = new Scanner(System.in);
  6. System.out.println("Type the number from which to start ");
  7. int from = in.nextInt();
  8. System.out.println("Type which number to finish ");
  9. int last = in.nextInt();
  10. System.out.println("Type step ");
  11. int step = in.nextInt();
  12. int n;
  13. if (from < last) {
  14. for (n = from; n <= last; n += step) {
  15. System.out.print(n + " ");
  16. if (step < 0) {
  17. throw new IllegalArgumentException();
  18. }
  19. }
  20. } else {
  21. for (n = from; n >= last; n += step) {
  22. System.out.print(n + " ");
  23.  
  24. }
  25. }
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement