applet factorial

 file name :Facto.java



import java.awt.*;

import java.applet.Applet;

import java.awt.event.*;

import javax.swing.*;


/*

<applet code="Facto.class" width="600" height="200">

</applet>

*/


public class Facto extends Applet implements ActionListener {

    Label l1, l2, l3;

    TextField t1, result;

    Button b1;


    public void init() {

        l1 = new Label("Enter a Number: ");

        add(l1);


        t1 = new TextField(10);

        add(t1);


        l3 = new Label("Result: ");

        add(l3);


        result = new TextField(10);

        result.setEditable(false);

        add(result);


        b1 = new Button("Factorial");

        add(b1);


        b1.addActionListener(this);

    }


    public void actionPerformed(ActionEvent e) {

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

            try {

                int value = Integer.parseInt(t1.getText());

                int fact = 1;


                for (int i = 1; i <= value; i++) {

                    fact *= i;

                }


                result.setText(String.valueOf(fact));

            } catch (NumberFormatException nfe) {

                JOptionPane.showMessageDialog(null, "Please enter a valid number!");

            } catch (ArithmeticException ae) {

                JOptionPane.showMessageDialog(null, "Arithmetic Exception occurred!");

            }

        }

    }

}






output :

Comments

Popular posts from this blog

SINGLE LINKED LIST by smd

CLL by smd

QUEUE USING ARRAYS by smd