Go to Top

Thursday 4 August 2011

To find the LCM of two numbers

Question 37 : Write a program in Java to find the Lowest Common Multiple (L.C.D.) of two numbers entered by the user :

Java Program : 

import java.io.*;
class lcm
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     int a,b,i;
     int c=0;
     System.out.print("Enter the first number : ");
     a=Integer.parseInt(br.readLine());
     System.out.print("\n");
     System.out.print("Enter the second number : ");
     b=Integer.parseInt(br.readLine());
     System.out.print("\n");
     c=a*b;
     int d=c;
     for(i=1;i<=c;i++)
     {
         if(i%a==0 && i%b==0 && i<d)
         d=i;
        }
        System.out.println("The L.C.M : "+d);
    }
}

28 comments:

  1. public class ques
    {
    public void main(int a, int b)
    {
    int l=a>b?a:b;
    int lcm=1;
    for(int i=l; i<=a*b; i++)
    {
    if(i%a==0 && i%b==0)
    {
    lcm=i;
    break;
    }
    }
    System.out.println(lcm+ " is lcm");
    }
    }

    ReplyDelete
    Replies
    1. you are loser not good program

      Delete
    2. This is a simple prg in bluej environment. Nice

      Delete
  2. kindly also give outputs as well thanks

    ReplyDelete
  3. hi u guys dont know programming stupid skankwad .....

    ReplyDelete
  4. import java.io.*;
    class LcmMethod_2
    {
    public static void main()throws IOException
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int a,b,max,min,x,lcm=1;
    System.out.print("Enter the 1st number : ");
    a=Integer.parseInt(br.readLine());
    System.out.print("Enter the 2nd number : ");
    b=Integer.parseInt(br.readLine());
    if(a>b)
    {
    max=a;
    min=b;
    }
    else
    {
    max=b;
    min=a;
    }
    /*
    To find the maximum and minimum numbers, you can also use
    int max=a>b?a:b;
    int min=a<b?a:b;
    */
    for(int i=1;i<=min;i++)
    {
    x=max*i; //finding multiples of the maximum number
    if(x%min==0) //Finding the multiple of maximum number which is divisible by the minimum number.
    {
    lcm=x; //making the first multiple of maximum number which is divisible by the minimum number, as the LCM
    break; //exiting from the loop, as we don’t need anymore checking after getting the LCM
    }
    }
    System.out.println("L.C.M. = "+lcm);
    }
    }


    ReplyDelete
  5. I HAVE 11 ERRORS.. WHAT TO DO...?

    ReplyDelete
    Replies
    1. USE THIS PROGRAM...
      import java.io.*;
      class lcm
      {
      static void main()throws IOException
      {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      int a,b,i;
      int c=0;
      System.out.print("Enter the first number : ");
      a=Integer.parseInt(br.readLine());
      System.out.print("\n");
      System.out.print("Enter the second number : ");
      b=Integer.parseInt(br.readLine());
      System.out.print("\n");
      c=a*b;
      int d=c;
      for(i=1;i<=c;i++)
      {
      if(i%a==0 && i%b==0 && i<d)
      d=i;
      }
      System.out.println("The L.C.M : "+d);
      }
      }

      Delete
  6. I DON'T UNDERSTAND YOUR CODING.. PLEASE GIVE SOME OTHER METHODS..

    ReplyDelete
  7. Calculating the Least Common Multiple (LCM) of two numbers involves finding the smallest multiple they share. My Stream Unwatchable It's an essential concept in mathematics.

    ReplyDelete

ShareThis