Java program to find out the HCF and LCF.



import java.util.*;
class Hcf {
 public static void main(String args[]) {
  int a, b;
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter two nos :");
  a = sc.nextInt();
  b = sc.nextInt();
  int big;
  int small;
  if (a > b) {
   big = a;
   small = b;
  } else {
   big = b;
   small = a;
  }
  for (int i = 1; i <= big; i++) {
   if (((big * i) % small) == 0) {
    int lcm = big * i;
    System.out.println("The least common multiple is " + (lcm));
    break;
   }
  }
  int temp = 1;
  while (temp != 0) {
   temp = big % small;
   if (temp == 0) {
    System.out.println("GCD is " + small);
   } else {
    big = small;
    small = temp;
   }
  }
 }
}


No comments:

Post a Comment