j.Develop swing application to display factorial of given number.

CODE:

 

package swing_practicals;

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

class fact extends JFrame implements ActionListener

{

    JLabel lb1;

    JTextField j1;

    JButton btn;

    JPanel jf;

    fact()

    {

        setDefaultCloseOperation(3);

        pack();

        setSize(300,200);

        setVisible(true);

        lb1 = new JLabel("Enter a number:");

        j1 = new JTextField(15);

        btn = new JButton("Fact");

        jf = new JPanel();

        jf.add(lb1);

        jf.add(j1);

        jf.add(btn);

        btn.addActionListener(this);

        getContentPane().add(jf,BorderLayout.CENTER);

    }

    public void actionPerformed(ActionEvent e)

    {

        int num = Integer.parseInt(j1.getText());

            

        int i,fact=1;      

        for(i=1;i<=num;i++){    

            fact=fact*i;    

        }   

        if(e.getSource() == btn){

            JOptionPane.showMessageDialog(null,"Factorial of "+num+" is "+fact);

        }

    }

    public static void main(String[] args) {

        new fact();

    }

}

OUTPUT:

Programs based on AWT and Swing.

Post a Comment

0 Comments