Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class non_prime{
- static boolean isPrime(int n)
- {
- // Check if number is less than
- // equal to 1
- if (n <= 1)
- return false;
- // Check if number is 2
- else if (n == 2)
- return true;
- // Check if n is a multiple of 2
- else if (n % 2 == 0)
- return false;
- // If not, then just check the odds
- for (int i = 3; i <= Math.sqrt(n); i += 2)
- {
- if (n % i == 0)
- return false;
- }
- return true;
- }
- // Driver code
- public static void main(String args[]){
- non_prime np= new non_prime();
- int i,c=0;
- System.out.println("The non prime numbers between 101 and 150 are :");
- for(i=101;i<=150;i++)
- {
- if(np.isPrime(i)==false)
- {
- c++;
- if (c==10){
- System.out.println();
- c=0;
- }
- System.out.print(i+",");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment