Gcdandlcm
Gcdandlcm
You have to take two inputs and give hcf and lcm for those numbers.
Requirements:
TestCases:
HCF: 4
LCM: 24
Input: (0, 5)
HCF: 5
LCM: 0
Negative Values:
LCM: 108
Assumptions:
The inputs will be two integers, which can be positive, negative, or zero.
The HCF and LCM are defined as if the inputs were positive, so you may want to take the absolute value of the
inputs before calculating.
HCF and LCM are typically defined only for non-negative integers, so the inputs should ideally be non-negative. If
either input is zero, the HCF will be the other number, and the LCM will be considered as zero (LCM(0,n) = 0).
Program code:
import java.util.Scanner;
int num1,num2,hcf=0,lcm=0;
num1=sc.nextInt();
num2=sc.nextInt();
if(num1<0 || num2<0){
else{
for(int i=1;i<num2;i++){
hcf=i;
lcm=(num1*num2)/hcf;
System.out.println("lcm="+lcm);
System.out.println("hcf="+hcf);
Output:
Input:12 15
lcm=60 hcf=3