WHAT'S NEW?
Loading...

Different ways in finding prime and non-prime numbers in java

       A prime number is a number which is divisible by only two numbers: 1 and itself. So, if any number is divisible by any other number, it is not a prime number.

The following source code implements different ways in finding a prime numbers.

Source code: 

<option 1>


public static void main(String[]args){
        int num = 29;
        boolean flag = false;
        for(int i = 2; i <= num/2; ++i)
        {
            // condition for nonprime number
            if(num % i == 0)
            {
                flag = true;
                break;
            }
        }
        if (!flag)
            System.out.println(num + " is a prime number.");
        else
            System.out.println(num + " is not a prime number.");
    }
    

<option 2 using methods>



public static void main(String args[]){    
        checkPrime(1);  
        checkPrime(3);  
        checkPrime(17);  
        checkPrime(20);  
    }  
      static void checkPrime(int n){  
        int i,m=0,flag=0;      
        m=n/2;      
        if(n==0||n==1){  
         System.out.println(n+" is not prime number");      
        }else{  
         for(i=2;i<=m;i++){      
          if(n%i==0){      
           System.out.println(n+" is not prime number");      
           flag=1;      
           break;      
          }      
         }      
         if(flag==0)  { System.out.println(n+" is prime number"); }  
        }//end of else  
    }  

Lastly:

    public static void main(String[]args){
        get(10);   
    }
    static void get(int n){
        for (int x=0; x<=n; x++){
            for (int j=2; j<x; j++){
                if (x%j==0){
                    message(x +" is not a Prime number, because " + x + "*" + x/j + "=" + x);
                    break;
                }else{
                    message(x + " is a Prime Number");
                    break;
                }
            } 
        }
    }
    static void message(String myText){
        System.out.println(myText);
    } 

Same output:

public static void main(String[]args){
        get(10);   
    }
    static void get(int n){
        for (int x=0; x<=n; x++){
                if (x%2==0){
                    message(x +" is not a Prime number, because " + x + "*" + x/2 + "=" + x);
                    continue;
                }
                message(x + " is a Prime Number");
        }
    }
    static void message(String myText){
        System.out.println(myText);
    }   


Boolean:

public static void main(String[] args)  
    { 
         if(isPrime(11))  
         System.out.println(" true") ; 
          
         else 
         System.out.println(" false"); 
           
    } 
       // is prime or not 
       static boolean isPrime(int n) 
       { 
           // Corner case 
           if (n <= 1
               return false
          
           // Check from 2 to n-1 
           for (int i = 2; i < n; i++) 
               if (n % i == 0
                   return false
          
           return true
       }    

1 comment: Leave Your Comments