Welcome To The World Of Java

This site will teach you the basics of Java. It is not necessary to have any prior programming experience.

Java program to find out the HCF and LCF.

Somdeb Burman | 11:32:00 AM | 0 comments



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;
   }
  }
 }
}

Java Resources are available for Download from below

Category: , , ,

0 comments