Java Program to check if a number is prime
Prime.txt
File size: 884 Bytes
File content type: text/plain
Category: Practical Files
Subject: Java Programming
/**
*
* @program To check if entered number is prime or not
*/
import java.io.*;
class Prime {
public static void main(String args[]) throws IOException {
BufferedReader k = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number");
int num = Integer.parseInt(k.readLine());
if(isPrime(num))
System.out.println(num + " is a prime number");
else
System.out.println(num + " is not a prime number");
}
static boolean isPrime(int n) {
//since 2 is the only even prime number
if(n > 2 && n % 2 == 0)
return false;
//check odd numbers that are prime
for(int i = 3;i < Math.sqrt(n)+1 ; i=i+2){
if(n%i == 0)
return false;
}
return true;
}
}
Java Program for prime testing.
Last Updated: July 16, 2022
Related
IPU CA Notes Unit 3
Category: NotesJuly 16, 2022
BBA Digital Marketing Notes Unit 2
Category: NotesJuly 17, 2022
RM Notes Unit 3 PDF
Category: NotesJuly 17, 2022
Management Accounting Notes Unit 2
Category: NotesJuly 17, 2022
Human Resource Management Notes Unit 1
Category: NotesJuly 17, 2022
IPU IBM Notes Unit 3
Category: NotesJuly 17, 2022
Sales Distribution Management Notes PDF
Category: NotesJuly 17, 2022
POM Unit 3 Notes PDF
Category: NotesJuly 17, 2022
Queue Implementation Using Array C Program
Category: Practical FilesJuly 16, 2022
Business Environment Notes Unit 3
Category: NotesJuly 17, 2022