Advertisement
Guest User

Untitled

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