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
Diffraction Notes
Category: NotesJuly 16, 2022
Java Security Notes
Category: NotesJuly 16, 2022
IPU Entrepreneurship Development Notes Unit 2
Category: NotesJuly 17, 2022
Unit 1 - Fuels
Category: NotesJuly 16, 2022
Manufacturing Processes Notes (Welding and Sheet Metal)
Category: NotesJuly 16, 2022
Computer Graphics and Multimedia(CGMT) Question Paper Bhavya 2015
Category: Question PapersJuly 16, 2022
Manufacturing Process Question Paper Akash 2011-2014
Category: Question PapersJuly 16, 2022
Data Mining Practical File Exp-8-11
Category: Practical FilesJuly 16, 2022
Java Program to print Fibonacci series upto n terms
Category: Practical FilesJuly 16, 2022
Productions and Operations Management Notes Unit 2
Category: NotesJuly 17, 2022