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