Skip to content

Instantly share code, notes, and snippets.

@ubshreenath
Created February 10, 2021 16:51
Show Gist options
  • Save ubshreenath/43fdff3106441acf2431911618a71b74 to your computer and use it in GitHub Desktop.
Save ubshreenath/43fdff3106441acf2431911618a71b74 to your computer and use it in GitHub Desktop.
A Java code to check whether a number is prime or not...
import java.io.*;
class PrimeNumber
{
public static void main(String args[])throws IOException
{
BufferedReader ob = new BufferedReader (new InputStreamReader (System.in));
int i,j=2;
System.out.println("Enter the number you want to check whether its prime or not...");
i=Integer.parseInt(ob.readLine());
if (i == 0)
{
System.out.println("You entered zero....invalid input");
}
else
{
for(j=2;j<i;j++)
{
int k = i%j;
if(k == 0)
{
System.out.println("Not Prime !");
break;
}
}
if(j == i)
{
System.out.println("Prime Number");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment