aneliabogeva

SumPrimeNonPrime

Nov 27th, 2020
572
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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×