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 scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- int sumPrime = 0;
- int sumNotprime = 0;
- while (!input.equals("stop")) {
- int numberInput = Integer.parseInt(input);
- if (numberInput < 0) {
- System.out.println("Number is negative.");
- input = scanner.nextLine();
- continue;
- }
- int counter =0;
- for (int i = 1; i <=numberInput ; i++) {
- if (numberInput%i==0){
- counter++;
- }
- }
- if (counter>2){
- sumNotprime +=numberInput;
- }else{
- sumPrime +=numberInput;
- }
- input = scanner.nextLine();
- }
- System.out.println("Sum of all prime numbers is: " + sumPrime);
- System.out.println("Sum of all non prime numbers is: " + sumNotprime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement