Prime Number Verification : Java program code along with first 100 prime numbers

Prime Number is one of the fundamental concepts of mathematics .Prime number can be defined as :
The number which is divisible only by itself and 1 . That is other than two numbers (itself and 1) no other number can give remainder 0 .

 But still when comes to coding prime number coding in Graphical user interface representation , its not that easy .


for e.g

23 , divisible by 1 and 23 only , thus its a prime number
20, divisible by 1,2,4,5,10.20 , thus it is not a prime number

Read Also :  Armstrong Number Java swing code with Example


Best efficient method to compute the prime number is to square root the number .  That is , if the number is prime then there will be no multiple of the number between 1 and the square root of the number .
Let us understand above statement with an example

Suppose we want to check whether 7687  is a prime number or not
So applying above method ,
Observing the number , the square root should lie between 80 (square of 80 is 6400) and 90 (square of 90 is 8100) . So we conclude that the square root of 7687 is smaller than 90 .
Now we just need to divide the given number 7687 from 2 to 90 , if any number is a multiple of the number
,than , the number is not prime , else the number is prime .
After you do calculation for the above given number 7687 , you will find that the number is prime.

Pseudo code :

1. Find out the square root of  the given number
2. Now consider square root value as the upper limit or the value upto which we need to decide whether the
    given number is prime or not .
3. Use hit and trial method , and divide the given number (for eg. 7687)  from 2 to the square root of
    value(90)  .
4. If there is any value which gives 0 remainder or a multiple of given number then the number is not prime
    and exit .    
5. If value reaches equal to the square root value i.e upper limit and there is no value whose remainder is 0
    or multiple or factor of the given value ,then number is  prime and exit .

List of Prime numbers upto 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


Read Also  :   Fibonacci series Recursion  program in java : Code with example 



prime number gui java code output image




Demo :

















so , when you run the program in the command prompt , then a gui pops up . Enter the number which you need to check , whether its a prime number or not , and then click ok . The text field with label Prime will automatically show the result by rendering true or false in the text field.

 
Code 



import java.awt.*;
import java.awt.event.*;


public class prime implements ActionListener,KeyListener
{
    Frame f;
    Label one,two;
    TextField three,four;
    Button five;
    int i,t,j;
    String s1="";
    public prime()
    {
        f=new Frame("Prime Number");
        one=new Label("Enter the number");
        two=new Label("Prime  ");
        three=new TextField(5);
        four=new TextField(5);
        five=new Button("OK");
        f.setSize(400,400);
        f.setVisible(true);
        f.add(one);
        f.add(two);
        f.add(three);
        f.add(four);
        f.add(five);
        f.setLayout(null);
        one.setBounds(20,20,140,40);
        two.setBounds(20,80,140,40);
        three.setBounds(180,30,140,40);
        four.setBounds(180,85,140,40);
        five.setBounds(240,240,40,40);
        three.addKeyListener(this);
        five.addActionListener(this);
    }
    public void keyPressed(KeyEvent k)
    {
        System.out.print("");
    }
    public void keyTyped(KeyEvent k)
    {
        s1+= k.getKeyChar();
    }
    public void keyReleased(KeyEvent k)
    {
        System.out.print("");
    }
    public void actionPerformed(ActionEvent ae)
    {
        t=Integer.parseInt(s1);
        for(i=1;i<t;i++)
        {
            if((i*i)>t)
            break;
        }
        for(j=2;j<i;j++)
        {
            if((t%j)==0)
            {
                four.setText("false");
                break;
            }
            if(j==(i-1))
            four.setText("true");
        }
    }
    
    public static void main(String s[])
    {
        new prime();
    }
}

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry