Advertisement
veronikaaa86

03. Sum Prime Non Prime

Feb 13th, 2022
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. package NestedLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P03SumPrimeNonPrime {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. String input = scanner.nextLine();
  10. int primeSum = 0;
  11. int nonPrimeSum = 0;
  12. while (!input.equals("stop")) {
  13. int currentNum = Integer.parseInt(input);
  14.  
  15. if (currentNum < 0) {
  16. System.out.println("Number is negative.");
  17. input = scanner.nextLine();
  18. continue;
  19. }
  20.  
  21. int count = 0;
  22. for (int i = 1; i <= currentNum; i++) {
  23. if (currentNum % i == 0) {
  24. count++;
  25. }
  26. }
  27.  
  28. if (count == 2) {
  29. primeSum = primeSum + currentNum;
  30. } else {
  31. nonPrimeSum = nonPrimeSum + currentNum;
  32. }
  33.  
  34. input = scanner.nextLine();
  35. }
  36. System.out.printf("Sum of all prime numbers is: %d%n", primeSum);
  37. System.out.printf("Sum of all non prime numbers is: %d", nonPrimeSum);
  38. }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement