joseleonweb

Untitled

Aug 23rd, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SumaInterval {
  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.     int acumulador = 0;
  16.  
  17.     if (n1 < n2) {
  18.       for (int i = n1; i <= n2; i++) {
  19.         acumulador = acumulador + i;
  20.       }
  21.       System.out.println("La suma total és " + acumulador + ".");
  22.     } else {
  23.       System.out.println("n2 no és més gran que n1!");
  24.     }
  25.   }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment