Design an AWT application to check whether the number entered in textbox is prime or not.


Source Code:-


import java.awt.*;


import java.awt.event.*;


class Prime extends Frame implements ActionListener


{


        TextField tf1,tf2;


        public Prime()


        {


 


                setLayout(new FlowLayout());


                Label lb1=new Label("Enter Number:");


                Label lb2=new Label("The Number is:");


 


                tf1=new TextField(15);


                tf2=new TextField(15);


 


                Button btn1=new Button("Calculate");


 


                add(lb1);


                add(tf1);


                add(lb2);


                add(tf2);


                add(btn1);


 


                btn1.addActionListener(this);


        }


        public static void main(String ar[])


        {


                Prime fr=new Prime();


                fr.setSize(300,300);


                fr.setTitle("Calculate Prime");


                fr.setVisible(true);


        }


 


        public void actionPerformed(ActionEvent ae)


        {


        int flag = 0,num;


                num=Integer.parseInt(tf1.getText());


        for(int i = 2; i <= num/2; i++)


        {


            if(num % i == 0)


            {


                flag = 1;


                break;


            }


        }


        if (flag==0)


            tf2.setText("prime number.");


        else


             tf2.setText("not prime number.");


        }


}


 


 Output :-


 

Comments

Popular posts from this blog

Write a Java code to input height (in inches) and convert it into feet and inches.

Design an AWT application to calculate the factorial of a number.