//
Find the Greatest Common Divisior of two integers
// Java how to program, 5/e, Exercise 6.28
import javax.swing. * ;
import java.awt. * ;
import java.awt.event. * ;
public class GCalculation extends JApplet implements ActionListener {
int n1,n2,G;
JLabel N1Label,N2Lable,GLable;
JTextField N1Field,N2Field,GField;
JTextArea output;
public void init()
{
Container container=getContentPane();
container.setLayout(new FlowLayout());
N1Label= new JLabel("Enter the first integer:");
container.add(N1Label);
N1Field=new JTextField(10);
container.add(N1Field);
N2Lable= new JLabel("Enter the first integer:");
container.add(N2Lable);
N2Field=new JTextField(10);
container.add(N2Field);
GLable= new JLabel("The Greated Common Divisor is:");
container.add(GLable);
GField=new JTextField(10);
container.add(GField);
N2Field.addActionListener(this);
}
public void actionPerformed (ActionEvent event)
{
n1=Integer.parseInt(N1Field.getText());
n2=Integer.parseInt(N2Field.getText());
for(int i=1;i<=Math.min(n1,n2);i++)
{
if (n1%i==0&&n2%i==0)
G=i;
}
GField.setText(Integer.toString(G));
}
}
// Java how to program, 5/e, Exercise 6.28
import javax.swing. * ;
import java.awt. * ;
import java.awt.event. * ;
public class GCalculation extends JApplet implements ActionListener {
int n1,n2,G;
JLabel N1Label,N2Lable,GLable;
JTextField N1Field,N2Field,GField;
JTextArea output;
public void init()
{
Container container=getContentPane();
container.setLayout(new FlowLayout());
N1Label= new JLabel("Enter the first integer:");
container.add(N1Label);
N1Field=new JTextField(10);
container.add(N1Field);
N2Lable= new JLabel("Enter the first integer:");
container.add(N2Lable);
N2Field=new JTextField(10);
container.add(N2Field);
GLable= new JLabel("The Greated Common Divisor is:");
container.add(GLable);
GField=new JTextField(10);
container.add(GField);
N2Field.addActionListener(this);
}
public void actionPerformed (ActionEvent event)
{
n1=Integer.parseInt(N1Field.getText());
n2=Integer.parseInt(N2Field.getText());
for(int i=1;i<=Math.min(n1,n2);i++)
{
if (n1%i==0&&n2%i==0)
G=i;
}
GField.setText(Integer.toString(G));
}
}