0% found this document useful (0 votes)
4 views2 pages

Gcdandlcm

Gcd and lcm java

Uploaded by

4mt21cs182
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Gcdandlcm

Gcd and lcm java

Uploaded by

4mt21cs182
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Problem statement:

You have to take two inputs and give hcf and lcm for those numbers.

Requirements:

1)Take two numbers namely num1 and num2 .

2)Take two variables hcf and lcm.

TestCases:

Basic Test Cases:

Input: (8, 12)

HCF: 4

LCM: 24

Cases Involving Zero:

Input: (0, 5)

HCF: 5

LCM: 0

LCM: 0 (this case may be theoretically discussed)

Negative Values:

Input: (-12, 18)

HCF: 6 (use absolute 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;

public class Main

public static void main(String[] args) {

int num1,num2,hcf=0,lcm=0;

Scanner sc=new Scanner(System.in);

num1=sc.nextInt();

num2=sc.nextInt();

if(num1<0 || num2<0){

System.out.println("input should be positive");

else{

for(int i=1;i<num2;i++){

if(num1%i==0 && num2%i==0){

hcf=i;

lcm=(num1*num2)/hcf;

System.out.println("lcm="+lcm);

System.out.println("hcf="+hcf);

Output:

Input:12 15

lcm=60 hcf=3

You might also like