aneliabogeva

SumPrimeNonPrime

Nov 27th, 2020
548
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 command = scanner.nextLine();
  7.         Integer sumPrime = 0;
  8.         Integer sumNonPrime = 0;
  9.  
  10.         while(!"stop".equals(command)){
  11.             Integer number = Integer.parseInt(command);
  12.             Integer counter = 0;
  13.             if(number < 0){
  14.                 System.out.println("Number is negative.");
  15.             }else{
  16.                 for(int i = 1; i <= number; i++){
  17.                     if(number % i == 0){
  18.                         counter += 1;
  19.                     }
  20.                 }
  21.                 if(counter == 2){
  22.                     sumPrime += number;
  23.                 }else{
  24.                     sumNonPrime += number;
  25.                 }
  26.             }
  27.             command = scanner.nextLine();
  28.         }
  29.         System.out.printf("Sum of all prime numbers is: %d\n", sumPrime);
  30.         System.out.printf("Sum of all non prime numbers is: %d", sumNonPrime);
  31.     }
  32. }
  33.  
RAW Paste Data