Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package O7_NestedLoops.Exercise;
- import java.util.Scanner;
- public class Ex07SumOfPrimeAndNonPrimeNumbers {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String command = scanner.nextLine();
- int sumPrime = 0;
- int sumNonPrime = 0;
- while (!command.equalsIgnoreCase("stop")) {
- int n = Integer.parseInt(command);
- if (n < 0) {
- System.out.println("Number is negative.");
- n=0;
- }
- boolean isPrime = true;
- for (int x = 2; x <= n/2; x++) {
- if (n % x == 0) {
- isPrime = false;
- break;
- }
- }
- if (n==1) {
- isPrime=false;
- }
- if (isPrime) {
- sumPrime += n;
- } else {
- sumNonPrime += n;
- }
- command = scanner.nextLine();
- }
- System.out.printf("Sum of all prime numbers is: %d%n", sumPrime);
- System.out.printf("Sum of all non prime numbers is: %d%n", sumNonPrime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement