Develop an applet that receives an integer in one text field and compute its factorial value in another text field, when the button named ‘Compute” is clicked.
Program Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="FactApplet" width=500 height=250> </applet>*/
public class FactApplet extends Applet implements ActionListener
{
TextField num, res;
Button b1;
public void init()
{
setLayout(null);
Label l1=new Label("Enter the Number:");
l1.setBounds(10,50,100,30);
num=new TextField();
num.setBounds(200,50,100,30);
Label l2=new Label("The Factorial is :");
l2.setBounds(10,100,100,40);
res=new TextField();
res.setBounds(200,100,100,40);
b1=new Button("Compute");
b1.setBounds(200,150,70,40);
add(l1);
add(l2);
add(num);
add(res);
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if((ae.getSource())==b1)
{
int n=Integer.parseInt(num.getText());
int i,fact=1;
for(i=1;i<=n;i++)
fact=fact*i;
res.setText(" "+fact);
}
}
}
Post A Comment:
0 comments: