### Factorial of a Number - Java AWT - Internet & Web Programming Lab

Program:

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

class ex2 extends Frame implements ActionListener
{
Label l1,l2,l3;
TextField t1;
Button b1,b2;
ex2()
{
super("Factorial of a number");
setLayout(null);
setSize(500,400);
setVisible(true);
b1=new Button("Find");
b2=new Button("Clear");
l1=new Label("Enter the number:");
l2=new Label("The factorial is:");
l3=new Label(null);
t1=new TextField();
l1.setBounds(100,50,120,20);
t1.setBounds(240,50,50,20);
l2.setBounds(100,80,130,20);
l3.setBounds(240,80,50,20);
b1.setBounds(200,150,50,20);
b2.setBounds(270,150,50,20);
Toolkit t=getToolkit();
Dimension d=t.getScreenSize();
int h=(int) d.getHeight();
int w=(int) d.getWidth();
setLocation(w/4, h/4);
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}

public void actionPerformed(ActionEvent ae)
{
int n;
if(ae.getSource()==b1)
{
n=Integer.parseInt(t1.getText().trim());
l3.setText(Integer.toString(fact(n)));
}
else
{
t1.setText(null);
l3.setText(null);
t1.requestFocus();
}
}

int fact(int n)
{
int f=1;
for(int i=1;i<=n;i++)
{
f=f*i;
}
return f;
}

public static void main(String s[])
{
ex2 ob=new ex2();
}
}

Output:

nn@linuxmint ~/Desktop/java 7 \$ javac ex2.java
nn@linuxmint ~/Desktop/java 7 \$ java ex2
nn@linuxmint ~/Desktop/java 7 \$