Friday, April 13, 2012

How to print Prime numbers in Java or how to check if a number is prime or not is classical Java programming questions, mostly taught in Java programming courses. A number is called prime number if its not divisible by any number other than 1 or itself and you can use this logic to check whether a number is prime or not. This program is slightly difficult than printing even or odd number which are relatively easier Java exercises. This Simple Java program print prime number starting from 1 to 100 or any specified number. It also has a method which checks if a number is prime or not.

Code Example to print Prime numbers in Java

Here is complete sample code example to print prime numbers from 1 to any specified number. This Java program can also check if a number is prime or not as prime number checking logic is encapsulated in isPrime(int number) method.

importjava.util.Scanner;

/**
* Simple Java program to print prime numbers from 1 to 100 or any number.
* A prime number is a number which is greater than 1 and divisible
* by either 1 or itself.
*/publicclass PrimeNumberExample {

publicstaticvoid main(String args[]){

//get input till which prime number to be printedSystem.out.println("Enter the number till which prime number to be printed: ");int limit = newScanner(System.in).nextInt();

/*
* Prime number is not divisible by any number other than 1 and itself
* @return true if number is prime
*/publicstaticboolean isPrime(int number){for(int i=2; i<number; i++){if(number%i == 0){returnfalse;//number is divisible so its not prime}}returntrue;//number is prime now}}

Output:
Enter the number till which prime number to be printed:20
Printing prime number from 1 to 20235711131719

In this Java program we have two part, first part which is taking input from user to print prime numbers and other part is function isPrime(int number) which checks whether a number is prime or not. Java method isPrime(int number) can also be used anywhere in code because its encapsulated logic of checking prime number. There is also another popular Java question is "How to check if a number is prime or not?", isPrime() can be used there. its rather simple and just implement the logic of prime number in Java i.e. divides a number, starting from 2 till the number itself, if its divisible by another number means its not prime.

That's all on how to print prime numbers in Java or how to check if a number is prime or not. Its worth remembering logic that when a number is prime and how do you check for prime number. If you are doing homework then get an Idea but type the program yourself , that will give you thinking time on how this Java program is working and checking for prime numbers.

In order to check if a number N is prime or not; it is enough to check if the number is divisible by any number from 2 to SQRT(N). If it is divisible then it is not a prime number else it is a prime number.No need to check till (N - 1). This way you can improve time complexity of your code which is very important if the number N is huge. Say you have to generate 1st one million prime numbers.

few more questionhow to determine prime numbers in Javajava prime number, prime number java example, how to check prime numbers in Java, how to find prime numbers in Java, how to print prime numbers in Java. how to calculate prime numbers in Java. Write a program to display prime numbers in Java. write a program to check if a number is prime or not.

I remember those days when programs like check if number is even or odd, prime, armstrong or perfect square were popular. now days programmer doesn't pay much attention over programming but rather they just want to learn more and more languages and technology like Java, C++, JSP, ASP etc etc. If you asked them to write program they fail to do in any language because they just familiar with syntax not even with complete library.

Also you dont need to check each number. If you can not devide by 2 you can skeep even numbers:if (a % 2 == 0) return false;for (int i = 3; i < Math.sqrt(n); i+=2) if (a % i == 0) return false;return true;

If you want to store all primes below some N (as asked in the 30 programming questions page that brought me here), you only need to check whether a given number is divisible by the primes already found.

e.g.

You find 2, 3, 5.

To check 7 you only need to see if it's divisible by those.This works since if a number n is divisible by k, k is either prime or has a prime factor (say j) and thus only primes need to be tested. Sample code, which can be further sped up, written while I'm elbow deep in tomatoes and pasta but seemed to work.

Hey wait a minute the example just goes from 20. What about 21. It is not a prime number. The comment of 1-100 is false. This program just prints odd numbers. This code is broken. The sqrt of n is a better solution as others has suggested is better.

Using Sieve of Eratosthenes logic avoids most of the problems here, though it does take more than just a couple of lines to code. It is also 'limited' in the sense that you can only go so far before you will reach limits imposed by your hardware or programming language.