Arnab_Manna

non_prime_inrange(101-150)

Dec 7th, 2021
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. class non_prime{
  2.    
  3.         static boolean isPrime(int n)
  4.         {
  5.         // Check if number is less than
  6.         // equal to 1
  7.         if (n <= 1)
  8.             return false;
  9.         // Check if number is 2
  10.         else if (n == 2)
  11.             return true;
  12.         // Check if n is a multiple of 2
  13.         else if (n % 2 == 0)
  14.             return false;
  15.         // If not, then just check the odds
  16.         for (int i = 3; i <= Math.sqrt(n); i += 2)
  17.         {
  18.             if (n % i == 0)
  19.               return false;
  20.         }
  21.         return true;
  22.     }
  23.     // Driver code
  24.  
  25.          
  26.          
  27.        public static void main(String args[]){
  28.         non_prime np= new non_prime();
  29.         int  i,c=0;
  30.         System.out.println("The non prime numbers between 101 and 150 are :");
  31.         for(i=101;i<=150;i++)
  32.         {
  33.  
  34.             if(np.isPrime(i)==false)
  35.             {
  36.                             c++;
  37.             if (c==10){
  38.                 System.out.println();
  39.                 c=0;
  40.             }
  41.                 System.out.print(i+",");
  42.             }
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment