Advertisement
Spocoman

03. Sum Numbers

Aug 28th, 2024
44
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 SumNumbers {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int n = Integer.parseInt(scanner.nextLine());
  7.        int sum = 0;
  8.  
  9.         while (sum < n) {
  10.             int currentNum = Integer.parseInt(scanner.nextLine());
  11.             sum += currentNum;
  12.         }
  13.        
  14.         System.out.println(sum);
  15.     }
  16. }
  17.  
  18. ИЛИ:
  19.  
  20. import java.util.Scanner;
  21.  
  22. public class SumNumbers {
  23.     public static void main(String[] args) {
  24.         Scanner scanner = new Scanner(System.in);
  25.         int n = Integer.parseInt(scanner.nextLine());
  26.        int sum = 0;
  27.  
  28.         while (sum < n) {
  29.             sum += Integer.parseInt(scanner.nextLine());
  30.         }
  31.  
  32.         System.out.println(sum);
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement