Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Program to find LCM of 2 given numbers
- import java.util.Scanner;
- import java.io.*;
- public class lcm{
- public static void main(String arg[]){
- Scanner sc = new Scanner(System.in);
- boolean cont = true;
- while(cont){
- System.out.print("\nEnter the first number : ");
- int num1 = sc.nextInt();
- System.out.print("Enter the second number : ");
- int num2 = sc.nextInt();
- int res = LCM(num1, num2);
- System.out.println("The LCM of " + num1 + " and " + num2 + " is : " + res);
- System.out.print("Do you want to continue (1/0): ");
- int c = sc.nextInt();
- if(c == 0){
- cont = false;
- }
- }
- }
- // method to find gcd of 2 numbers
- static int gcd (int m1 , int m2)
- {
- if(m1==0 || m2==0)
- {
- return 0;
- }
- do
- {
- if(m1 > m2)
- {
- m1 = m1 - m2;
- }
- else
- {
- m2 = m2 - m1;
- }
- }while(m1!=m2);
- return(m1);
- }
- // method that finds lcm of numbers
- static int LCM(int num1, int num2){
- int res = gcd(num1, num2);
- int lcm_num = (num1 * num2) / res;
- return lcm_num;
- }
- }
- /*
- Enter the first number : 12
- Enter the second number : 14
- The LCM of 12 and 14 is : 84
- Do you want to continue (1/0): 1
- Enter the first number : 15
- Enter the second number : 25
- The LCM of 15 and 25 is : 75
- Do you want to continue (1/0): 0
- */
Add Comment
Please, Sign In to add comment