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
Applied Physics First Sem 2014 Akash GGSIPU
Category: Question PapersJuly 16, 2022
FM Notes Unit 2 PDF
Category: NotesJuly 17, 2022
C program for drawing a Circle using Midpoint Circle Algorithm - CGMT
Category: Practical FilesJuly 16, 2022
Java Applet Notes
Category: NotesJuly 16, 2022
CN Computer Networks Formatted Practical File
Category: Practical FilesJuly 16, 2022
Phase Rule Unit 2 Photocopied Class Notes PDF
Category: NotesJuly 16, 2022
IPU BBA FAA Notes Unit 4
Category: NotesJuly 16, 2022
International Business Management Notes Unit 4
Category: NotesJuly 17, 2022
Computer Fundamentals with no missing pages
Category: eBooksJuly 16, 2022
IPU BPS Notes Unit 3
Category: NotesJuly 17, 2022