Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SumPrimeNonPrime {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String input = scan.nextLine();
- int primeSum = 0;
- int nonPrimeSum = 0;
- while (!"stop".equals(input)) {
- int currentNumber = Integer.parseInt(input);
- if (currentNumber < 0) {
- System.out.println("Number is negative.");
- input =scan.nextLine();
- continue;
- }
- boolean isPrime = true;
- for (int i = 2; i < currentNumber; i++) {
- if (currentNumber % i == 0) {
- isPrime = false;
- break;
- }
- }
- if(isPrime) {
- primeSum +=currentNumber;
- } else {
- nonPrimeSum +=currentNumber;
- }
- input = scan.nextLine();
- }
- System.out.printf("Sum of all prime numbers is: %d%nSum of all non prime numbers is: %d", primeSum, nonPrimeSum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement