Advertisement
CoineTre

03. Sum Prime Non Prime

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