Computer Science, asked by fazlur19, 4 months ago

Write a Program in Java to input 2 numbers and find their Greatest Common Divisor

(GCD) and LCM(Least common Multiple

It's very urgent please do the question ​

Answers

Answered by samhi71
0

import java.util.Scanner;

public class JavaExample{

public static void main(String args[]){

int temp1, temp2, num1, num2, temp, hcf, lcm;

Scanner scanner = new Scanner(System.in);

System.out.print("Enter First Number: ");

num1 = scanner.nextInt();

System.out.print("Enter Second Number: ");

num2 = scanner.nextInt();

scanner.close();

temp1 = num1;

temp2 = num2;

while(temp2 != 0){

temp = temp2;

temp2 = temp1%temp2;

temp1 = temp;

}

hcf = temp1;

lcm = (num1*num2)/hcf;

System.out.println("HCF of input numbers: "+hcf);

System.out.println("LCM of input numbers: "+lcm);

}

}

mark my answer as brainliest

Answered by samarthkrv
0

Answer:

import java.util.*;

public class Main

{

   static int gcd(int a , int b){

       ArrayList<Integer> list = new ArrayList<Integer>();

       if(a > b){

           int half = a/2;

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

               if(a % i == 0 && b % i == 0){

                   list.add(i);

               }

           }

       }

       if(b > a){

           int half = b/2;

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

               if(a % i == 0 && b % i == 0){

                   list.add(i);

               }

           }

       }

       return Collections.max(list);

   }

public static void main(String[] args) {

    System.out.println(gcd(5,3));

}

}

Explanation:

Similar questions