Thursday, July 9, 2015

Least Common Multiple (LCM) Solver in Java

This sample program that I wrote in Java programming language will ask the user to enter two numbers and then our program will compute and determine the least common multiple or LCM of the two numbers being provided by our user.


Feel free to use my code in your programming projects at school or work.  If you have some questions please send me an email at jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

People here in the Philippines can reach me at my mobile number 09173084360.

Thank you very much and Happy Programming.




Sample Program Output

Program Listing

// lcm.java
// Written By: Mr. Jake R. Pomperada, BSCS, MAED-IT
// July 9, 2015   Thursday
// Email Address:  jakerpomperada@gmail.com 
//                 jakerpomperada@yahoo.com

import java.util.Scanner;

public class lcm {

    public static void find_lcm(int x, int y)
    {
        int max=0,min=0,z=0,lcm=1;
  if(x>y)
    {
    max=x;
    min=y;
    }
    else
    {
    max=x;
    min=y;
    }

for(int i=1;i<=min;i++)
   {
    z=max*i; 
    if(z%min==0) 
     {
      lcm=z; 
      break; 
     }
    }
    System.out.println();
    System.out.println("The Least Common Multiple (LCM) of "+ x 
                   + " and " + y +  " is = " + lcm + ".");
    }

    
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
        
      char ch;
      int first_value=0;
      int second_value=0;
 do {
      System.out.println();
      System.out.print("\t LEAST COMMON MULTIPLE (LCM) SOLVER ");
      System.out.println("\n");
      
      System.out.print("Kindly give the first number  : "); 
      first_value = input.nextInt();
      
      System.out.print("Kindly give the second number : "); 
      second_value = input.nextInt();

      find_lcm(first_value,second_value);
      System.out.print("\nDo you want to continue (Type y or n) : ");
      ch = input.next().charAt(0);                        
     } while (ch == 'Y'|| ch == 'y');  
      System.out.println();
      System.out.print("\t THANK YOU FOR USING THIS PROGRAM");
      System.out.println("\n\n");
   }

}


No comments:

Post a Comment