Advertisement
joseleonweb

Untitled

Aug 23rd, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ParellsDinsInterval {
  4.   public static void main (String[] args) {
  5.     Scanner lector = new Scanner(System.in);
  6.  
  7.     System.out.print("Quin és el valor de n1? ");
  8.     int n1 = lector.nextInt();
  9.     lector.nextLine();
  10.  
  11.     System.out.print("Quin és el valor de n2 (més gran que n1)? ");
  12.     int n2 = lector.nextInt();
  13.     lector.nextLine();
  14.  
  15.     if (n1 < n2) {
  16.       int i = n2;
  17.       //Es comença pel primer parell més proper a n2, dins de l'interval.
  18.       if (n2%2 != 0) {
  19.         i--;
  20.       }
  21.       while (i >= n1) {
  22.         System.out.println(i);
  23.         //S'avança de dos en dos, per recórrer només els parells.
  24.         i = i - 2;
  25.       }
  26.     } else {
  27.       System.out.println("n2 no és més gran que n1!");
  28.     }
  29.   }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement