Sunday, January 30, 2022

GCD/HCF and LCM

 Euclidean Theorm: gcd (a, b) = gcd (b, a % b)

HCF * LCM = a * b



public class GCD_LCM {
public static void main(String[] args) {
int a = 36;
int b = 24;

int original_a = a, original_b = b;
while (b > 0) {
int rem = a % b;
a = b;
b = rem;
}

System.out.println("HCF: " + a);

System.out.println("LCM: " + original_a * original_b / a);


}
}

No comments:

Post a Comment

Popular Posts

Most Featured Post

GitHub: Squash all commits in PR

  Command Meaning git fetch origin Get the latest origin/master . git reset --soft origin/master Move your branch to match origin/master , b...