+ 1
To find LCM and HCF
write a program in java to accept four number and find LCM and HCF of the four number.
2 Antworten
+ 2
Errr, is a Python program okay? I am not good with Java
0
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;
}