SCHOOL OF CODE BUILDERS
Learn To CODE. Become A DEVELOPER.
Pages
HOME
DATA STRUCTURES
STRINGS
ARRAYS
MATRIX
BINARY TREES
LINKED LIST
STACK
QUEUE
SORTING
SEARCHING
C
PYTHON
PSEUDOCODE
CONTEST PROBLEMS
ALGORITHMS
PATTERNS
PHP
C PUZZLES
C INTERVIEW QUESTIONS
JAVA
C++
HASHING
RECURSION
BASIC C PROGRAMS
TCS-CODEVITA
FACEBOOK
CONTACT US
Java program to check prime number
Java program to check prime number
Output:
Enter a number to check for prime number: 8
8 is not a prime number
Implementation:
import java.util.*; import java.lang.*; import java.io.*; class CodeRegister { public static void main (String[] args) throws java.lang.Exception { System.out.println("Enter a number to check for prime number:"); Scanner in=new Scanner(System.in); int n=in.nextInt(); if(checkprime(n)) { System.out.println(n + " is a prime number "); } else { System.out.println(n + " is not a prime number "); } } public static boolean checkprime(int n) { int i; if(n<=1) return false; if(n>=2 && n<=3) return true; if(n%2==0 || n%3==0) return false; else { for(i=5;i*i<=n;i+=6) { if(n%i==0 || n%(i+2)==0) return false; } return true; } } }
PREVIOUS
NEXT
HOME