0
plz send the java programming to find HCF AND LCM OF TWO NUMBER USING SIMPLE JAVA
plz don't use package
2 Réponses
+ 1
import java.util.Scanner;
public class HCF_nNum {
public static void main(String[] args) {
int hcf=0;
int lcm=0;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter Number1: ");
int num1 = scan.nextInt();
System.out.println("Please enter Number2: ");
int num2 = scan.nextInt();
System.out.println("Please enter Number3: ");
int num3 = scan.nextInt();
System.out.println("Please enter Number4: ");
int num4 = scan.nextInt();
scan.close();
hcf = hcfNum((hcfNum(num1,num2)),(hcfNum(num3,num4)));
System.out.println("HCF of numbers: " + num1 +"," + num2 +","+ num3+ "," + num4 +" is : " + hcf);
lcm =lcmNum((lcmNum(num1,num2)),(lcmNum(num3,num4)));
System.out.println("LCM of numbers: " + num1 +"," + num2 +","+ num3+ "," + num4 +" is : " + lcm);
}
public static int hcfNum(int a, int b) {
int temp = 0;
int small = Math.min(a, b);
int big = Math.max(a, b);
while (small != 0) {
temp = small;
small = big%small;
big = temp;
}
return temp;
}
public static int lcmNum(int a, int b) {
int lcm=0;
try{lcm = (a*b)/hcfNum(a,b);}
catch (ArithmeticException e)
{ System.out.println("Zero should not be an input");
}
return lcm;
}
+ 1
import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[]) {
int a, b, x, y, t, hcf, lcm;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Two Number : ");
x = scan.nextInt();
y = scan.nextInt();
a = x;
b = y;
while(b != 0) {
t = b; b = a%b; a = t;
}
hcf = a;
lcm = (x*y)/hcf; System.out.print("HCF = " +hcf); System.out.print("\nLCM = "+lcm);
}
}