The perfect place for easy learning...

Java Programming Language

×

List of Programs


Java Practical Programs


Aim


Develop an applet in Java that receives an integer in one text field, and computes its factorial Value and returns it in another text field, when the button named “Compute” is clicked.

Implementation


Java Program
import java.awt.*; 
import java.awt.event.*;
import java.applet.Applet;

/*<applet code="Fact.class" height=300 width=300></applet>*/

public class Factorial extends Applet implements ActionListener
{
	Label inputLable,outputLable; 
	TextField inputTextField,outputTextField; 
	Button btn;
	public void init(){
		inputLable=new Label("Enter any integer value: "); 
		inputTextField=new TextField(5); 
		btn=new Button("Compute"); 
		btn.addActionListener(this);
		outputLable=new Label("Factorial of given integer number is ");
		outputTextField=new TextField(10); 
		add(inputLable);
		add(inputTextField);
		add(btn);  
		add(outputLable);
		add(outputTextField);
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==btn)
		{
			int fact=fact(Integer.parseInt(inputTextField.getText())); 
			outputTextField.setText(String.valueOf(fact));
		}
	}
	int fact(int f)
	{
		if(f==0) 
			return 1; 
		else
			return f*fact(f-1);
	}
}

Result



   Download Source Code

Place your ad here
Place your ad here