You are on page 1of 5

Practical No.

12

Title: Write a program to calculate factorial of a number. Make use of JTextField.


Name: Bisma Md.Mohasin Shaikh
Roll No.: 44 Class: TYCM-2 Course: AJP Date of Performance: 17/08/20
************************************************************************************
CODE:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
/*<applet code=JText_prac12 width=400 height=370></applet>*/
public class JText_prac12 extends JApplet implements ActionListener
{
JTextField tf1,tf2;
JButton b1;
JLabel l1,l2,l3;
public void init()
{ setLayout(new FlowLayout());
tf1=new JTextField(23);
tf2=new JTextField(23);
l1=new JLabel("Enter the number: ");
l2=new JLabel("Factorial");
b1=new JButton("Find Factorial");

l3=new JLabel();
add(l1);
add(tf1);
add(l2);
add(tf2);
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String s=tf1.getText();
int no=Integer.parseInt(s);
if(ae.getSource()==b1)
{
int fact=1;
while(no>1)
{
fact=fact*no;
no--;
}
tf2.setText(Long.toString(fact));
}
}
}
OUTPUT:
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner

You might also like