Java Program to print prime numbers from 1 to 100

Posted by Somesh Shinde On Sunday, 26 June 2016 0 comments

Java Program to print prime numbers from 1 to 100

In this article, I'll share you a simple problem about writing a Java program to print prime numbers up to a given number e.g. say prime numbers from 1 to 100. It's one of the most common coding exercises for programmers learning in Java, as it gives you an opportunity to learn more about essential operator in Java Programming. The key here is that you cannot use a library function which can simply your job, you need to devise the algorithm for checking prime number by yourself. One of the most popular algorithm for generating prime is Sieve of Eratosthenes, which we have discussed earlier, but in this post, we will take a simpler approach. We'll first write a function to check whether a number is prime or not and then we loop through first 100 numbers i.e. from 1 to 100 and print only those which passed the prime test.




How to check if a number is prime or not

A number is said to be prime if it's not divisible by any number other than itself e.g. 2, 3 or 5. 1 is not counted as a prime number, so the lowest prime number is 2. One of the easiest way to check whether a number is prime or not is to loop from 2 to the number itself and checks if it's divisible by any number in between or not.

You can do that check by using modulus operator in Java, which return zero if a number is perfectly divisible by another number. If the number you are checking is not divisible by anyone then it's a prime number otherwise, it's not a prime number.

But this logic can be further optimized to only loop through the square root of the number instead of the number itself, as shown in below example. This will make the Java program fast for checking large prime numbers.

Here is a list of all prime numbers between 1 and 100:
Java Program to print prime numbers from 1 to 100


An optimized way to generate Prime numbers from 1 to 100

/**
 * Java Program to print prime numbers from 1 to 100
 *
 * @author Javin Paul
 */
public class PrimeNumberGenerator {

    public static void main(String args[]) {

        // print prime numbers from 1 - 100
        System.out.println("Prime numbers from 1 to 100 ");

        for (int i = 2; i <= 100; i++) {
            if (isPrime(i)) {
                System.out.println(i);
            }
        }
    }

    /*
     * An optimized to check if a number is prime or not.
     */
    public static boolean isPrime(int num) {
        if (num == 2 || num == 3) {
            return true;
        }

        if (num % 2 == 0 || num % 3 == 0) {
            return false;
        }

        for (int i = 3; i < Math.sqrt(num); i += 2) {
            if (num % i == 0 || num % Math.sqrt(num) == 0) {
                return false;
            }
        }
        return true;

    }
}

Output:
Prime numbers from 1 to 100 
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

That's all about how to print prime numbers in Java from 1 to 100.  Let me know if you find any bug on this program or you think if this program will not work in any specific scenario. This looks much more optimized than looping till the number itself.

0 comments:

Post a Comment