programming code for prime number


Feel free to use it.. just wanna share this code if you really need it..

--------------------------------------------------------------------------------------------------------------------

package prime.number;

import java.util.Scanner;


public class PrimeNumber {

 
    public static void main(String[] args) {
        Scanner num = new Scanner (System.in);
        int value,calc;
        boolean prime=true;
        System.out.println("enter the number : ");
        value=num.nextInt();
        if (value==1)
            System.out.println("not a prime number");
        else{        
        for(int i=2;i<=value/2;i++)
{
           calc=value%i;
  if(calc==0)
  {
     prime=false;
     break;
  }
}
     
if(prime)
  System.out.println(value + " is a prime number");
else
  System.out.println(value + " is not a prime number");
        }
     
    }
 
}

------------------------------------------------------------------------------------------------------------------------


Wish me luck and can do my programming question tomorrow in Programming League !!

No comments :

Post a Comment

please be nice here and I will be nice to you..