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


Source Code:-


import java.awt.*;


import java.awt.event.*;


class Factorial extends Frame implements ActionListener


{


          TextField tf1,tf2;


          public Factorial()


          {



                   setLayout(new FlowLayout());


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


                   Label lb2=new Label("Factorial 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 arg[])


          {


 


                   Factorial fr=new Factorial();


                   fr.setSize(300,300);


                   fr.setTitle("Calculate Factorial");


                   fr.setVisible(true);


 


          }


 


          public void actionPerformed(ActionEvent ae)


 


          {


 


                   int n,f=1,i;


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


                   for(i=1;i<=n;i++)


 


                   {


                             f=f*i;


                   }


 


                   tf2.setText(" "+f);


          }


 


}




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 check whether the number entered in textbox is prime or not.