Advertisement
Deianov

07.4.07. Sum Prime Non Prime

Oct 23rd, 2018
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. // 07.4.07. Sum Prime Non Prime
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P07 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String input = scanner.nextLine();
  9.         int sumPrime = 0;
  10.         int sumNot = 0;
  11.         while (!"stop".equals(input)) {
  12.             int num = Integer.parseInt(input);
  13.             if (num < 0) {
  14.                 System.out.println("Number is negative.");
  15.             } else {
  16.                 boolean isPrime = true;
  17.                 if (num >= 2) {
  18.                     for (int i = 2; i * i <= num; i++) {
  19.                         if (num % i == 0) {
  20.                             isPrime = false;
  21.                         }
  22.                     }
  23.                 } else {
  24.                     isPrime = false;
  25.                 }
  26.                 if (isPrime) sumPrime += num;
  27.                 else sumNot += num;
  28.             }
  29.             input = scanner.nextLine();
  30.         }
  31.         System.out.println("Sum of all prime numbers is: " + sumPrime);
  32.         System.out.println("Sum of all non prime numbers is: " + sumNot);
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement