Advertisement
icatalin

Lab 1 JAVA (neterminat) 19.02.2019

Feb 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.66 KB | None | 0 0
  1. public class Main {
  2.  
  3.     public static boolean  isPrime(int n) {
  4.         //check if n is a multiple of 2
  5.         if (n%2==0) return false;
  6.         //if not, then just check the odds
  7.         for(int i=3;i*i<=n;i+=2) {
  8.             if(n%i==0)
  9.                 return false;
  10.         }
  11.         return true;
  12.     }
  13.  
  14.  
  15.  
  16.  
  17.     public static  void main(String[] args){
  18.         int sum = 0;
  19.  
  20.         for (int i = 0; i < args.length; i++) {
  21.                 int number = Integer.parseInt(args[i]);
  22.             if (isPrime(number))
  23.                 System.out.println(number);
  24.  
  25.             sum = sum + number;
  26.         }
  27.  
  28.  
  29.  
  30.         System.out.println(sum);
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement