Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Check A Number is Prime Or Not

Article:
Viewed:  937 
Posted On:  23/07/2013 12:23:16 
In this article i am going to show you how we can check the number given by user is prime or not......... 

 

Below is my code:

import java.util.Scanner;

class prime
{
    public static void main(String s[]){
    int a,b;
    System.out.println("enter no. to check prime");
    Scanner scan=new Scanner(System.in);
    a=scan.nextInt();
   
   System.out.println("you entered "+a);
   
    for(b=2;b<=a-1;b++)
    {
if(a%b==0)
{
System.out.println("no. is not prime");
        break;
}
    }

    if(a==b)
    {
          System.out.print("prime");}
     }
} 

      Step 1:  After writing this in notepad save the file with the name followed by (.java) for eg: I saved it with Prime.java

Step 2 : 
Open command prompt 

Step 3 : Write javac Prime.java - Press Enter for compilation

Step 4 : Write java Prime for execution

Output:

Check A Number Is Prime or Not.png

Image 1.

  Comment:
 
By Umesh  On  20/10/2014 03:27:26
Hello Good Article You can increase performance check number upto num/2 .. then it will reduce time complexity by half time. for(b=2;b<=a/2;b++) { if(a%b==0) { System.out.println("no. is not prime"); break; } } Thanks Umesh Happy Coding
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us