Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package S6_NestedLoops;
- import java.util.Scanner;
- public class SumPrimeNonPrime {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int sumPrimeNumbers = 0;
- int sumNonPrimeNumbers = 0;
- String command = scanner.nextLine();
- while (!command.equals("stop")) {
- int currentNum = Integer.parseInt(command);
- int dividersCount = 0;
- if (currentNum < 0) {
- System.out.println("Number is negative.");
- command = scanner.nextLine();
- continue;
- }
- for (int i = 1; i <= currentNum; i++) {
- if(currentNum % i == 0) {
- dividersCount++;
- }
- }
- if(dividersCount > 2) {
- sumNonPrimeNumbers += currentNum;
- } else {
- sumPrimeNumbers += currentNum;
- }
- command = scanner.nextLine();
- }
- System.out.printf("Sum of all prime numbers is: %d%n" +
- "Sum of all non prime numbers is: %d", sumPrimeNumbers, sumNonPrimeNumbers);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement