plz send the java programming to find HCF AND LCM OF TWO NUMBER USING SIMPLE JAVA | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

plz send the java programming to find HCF AND LCM OF TWO NUMBER USING SIMPLE JAVA

plz don't use package

13th Feb 2018, 1:38 PM
Rakesh Singg
Rakesh Singg - avatar
2 Answers
+ 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; }
28th Feb 2018, 8:09 PM
Mudita Saini
+ 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); } }
27th Apr 2018, 3:00 AM
Akhil CH
Akhil CH - avatar