0% found this document useful (0 votes)
159 views2 pages

HCF Using Applet:: 1. Without Using Button - After Giving Input Press Enter

This document describes two Java applets for calculating the highest common factor (HCF) of two numbers. The first applet allows the user to directly enter two numbers into text fields and presses enter to calculate the HCF. It displays the input, output, and calculation process. The second applet uses a button to trigger the HCF calculation after entering two numbers into text fields. It displays labels and text fields for the two inputs and one output, and uses the button's action listener to call the HCF method and display the result.

Uploaded by

Smita Sarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views2 pages

HCF Using Applet:: 1. Without Using Button - After Giving Input Press Enter

This document describes two Java applets for calculating the highest common factor (HCF) of two numbers. The first applet allows the user to directly enter two numbers into text fields and presses enter to calculate the HCF. It displays the input, output, and calculation process. The second applet uses a button to trigger the HCF calculation after entering two numbers into text fields. It displays labels and text fields for the two inputs and one output, and uses the button's action listener to call the HCF method and display the result.

Uploaded by

Smita Sarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

HCF USING APPLET :

1. Without using button . after giving input press enter.

/* <applet code="Hcf.class" height=800 width =800></applet> */

import java.awt.*;
import java.applet.*;

public class Hcf extends Applet


{
TextField T1,T2;

public void init() {


T1 = new TextField(10);
T2 = new TextField(10);
add(T1);
add(T2);
T1.setText("0");
T2.setText("0");
}

public void paint(Graphics g) {


int a, b, result;
String str;

g.drawString("Enter Number in TextField to Find HCF of 2 No.s


",10,50);
g.setColor(Color.red);
str=T1.getText();
a=Integer.parseInt(str);
str=T2.getText();
b=Integer.parseInt(str);
result=hcf(a,b);
g.drawString("HCF Result is : "+result,10,80);
showStatus("Hcf of 2 Numbers");
}

int hcf(int a, int b)


{
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
return a;
}
public boolean action(Event e, Object o){
repaint();
return true;
}
}
2. using button . after giving input press button.

/* <applet code="Hcf2.class" height=800 width =800></applet> */

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.applet.*;

public class Hcf2 extends Applet implements ActionListener


{

TextField t1 = new TextField(10);


TextField t2 = new TextField(10);
TextField t3 = new TextField(10);
Label l1 = new Label("FIRST NO=:");
Label l2 = new Label("SECOND NO:");
Label l3 = new Label("HCF:");
Button b = new Button("CALCULATE");
public void init()
{

add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == b)
{
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int res=hcf(n1,n2);
t3.setText(" " +res);
}
}

int hcf(int a, int b)


{
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
return a;
}

You might also like