Skip to content

Instantly share code, notes, and snippets.

@Techgokul
Created June 29, 2018 17:52
Show Gist options
  • Save Techgokul/c61b63abcf66a57a78c6eea6731bc750 to your computer and use it in GitHub Desktop.
Save Techgokul/c61b63abcf66a57a78c6eea6731bc750 to your computer and use it in GitHub Desktop.
Check whether a number is Prime or not
import java.util.Scanner;
/**
*
* @author tech gokul
*/
public class PrimeNum {
public static void main(String[] args) {
int num, i = 2;
boolean flag = false;
System.out.println("Enter the number to check the number is prime or not: ");
Scanner in= new Scanner(System.in);
num= in.nextInt();
while(i <= num/2)
{
// condition for nonprime number
if(num % i == 0)
{
flag = true;
break;
}
++i;
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment