LCM of Three Numbers

How to Find LCM of three numbers

NOTE:- Please comment down the code in other languages as well below –

12 comments on “LCM of Three Numbers”


  • Rajat

    import java.util.*;

    public class Main {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int m = sc.nextInt();
    int a = sc.nextInt();
    int gcd =1;

    for(int i = 2; i<=n && i<=m ; i++){
    if(n%i==0 && m%i==0 ){
    gcd = i;
    }
    }
    int Lc =(n*m)/gcd;

    for(int i =2; i<=Lc && i<=a; i++){
    if(Lc%i==0 && a%i==0){
    gcd=i;
    }
    }
    int Lcm =(Lc*a)/gcd;
    System.out.println(Lcm);
    }
    }


  • TRAVIS

    #include
    int main(){
    int num1, num2;
    printf(“Input your number 1:”);
    scanf(“%d”, &num1);
    printf(“Input your number 2:”);
    scanf(“%d”, &num2);
    //find max and main
    int max = num1, min = num2;
    if(num1 < num2){
    max = num2;
    min = num1;
    }
    int k = 1, LCM;
    do{
    LCM = max*k;
    k++;
    }while(LCM%min == 0);
    printf("LCM of %d and %d is %d", num1 ,num2, LCM);
    }


  • 19EC052

    class CalculateLCM
    { public static void main(String s[])
    {
    int result = calculateLcm(5, 10, 15);
    System.out.println(“The Lowest Common Multiple is ” + result);

    }

    public static int calculateLcm(int first, int second, int third)
    {
    int result = 0;
    //Write a code here to calculate the LCM of three numbers and assign it to result.
    return result;
    }
    }


  • Navmi

    n=int(input(“n”))
    m=int(input(“m”))
    p=int(input(“p”))

    if(m>n and m>p):
    lg=m
    elif(n>p and n>m):
    lg=n
    else:
    lg=p

    while(True):
    if(lg%n==0 and lg%m==0 and lg%p==0):
    print(lg)
    break
    lg+=1


  • abp6394

    LCM of three numbers using function: #include int LCM_of_three_num(int n1, int n2, int n3)
    {
    int LCM, K;
    if(n1 > n2 && n1 > n3)
    {
    LCM = n1;
    }
    else if(n2 > n1 && n2 > n3)
    {
    LCM = n2;
    }
    else if(n3 > n1 && n3 > n2)
    {
    LCM = n3;
    } K = LCM; while(1)
    {
    if(LCM%n1==0 && LCM%n2==0 && LCM%n3==0)
    {
    break;
    }
    else
    {
    LCM = LCM + K;
    }
    } return LCM;
    } int main()
    {
    int n1, n2, n3, Result;
    scanf(“%d %d %d”, &n1, &n2, &n3); Result = LCM_of_three_num(n1, n2, n3); printf(“LCM(%d, %d, %d) = %d”, n1, n2, n3, Result);
    } OUTPUT:
    12 15 10
    LCM(12, 15, 10) = 60


  • swati

    LCM of three numbers using function:

    #include

    int LCM_of_three_num(int n1, int n2, int n3)
    {
    int LCM, K;
    if(n1 > n2 && n1 > n3)
    {
    LCM = n1;
    }
    else if(n2 > n1 && n2 > n3)
    {
    LCM = n2;
    }
    else if(n3 > n1 && n3 > n2)
    {
    LCM = n3;
    }

    K = LCM;

    while(1)
    {
    if(LCM%n1==0 && LCM%n2==0 && LCM%n3==0)
    {
    break;
    }
    else
    {
    LCM = LCM + K;
    }
    }

    return LCM;
    }

    int main()
    {
    int n1, n2, n3, Result;
    scanf(“%d %d %d”, &n1, &n2, &n3);

    Result = LCM_of_three_num(n1, n2, n3);

    printf(“LCM(%d, %d, %d) = %d”, n1, n2, n3, Result);
    }

    OUTPUT:
    12 15 10
    LCM(12, 15, 10) = 60


  • swati

    #include

    int main()
    {
    int n1, n2, n3, LCM, K;
    scanf(“%d %d %d”, &n1, &n2, &n3);

    if(n1 > n2 && n1 > n3)
    {
    LCM = n1;
    }
    else if(n2 > n1 && n2 > n3)
    {
    LCM = n2;
    }
    else if(n3 > n1 && n3 > n2)
    {
    LCM = n3;
    }

    K = LCM;

    while(1)
    {
    if(LCM%n1==0 && LCM%n2==0 && LCM%n3==0)
    {
    break;
    }
    else
    {
    LCM = LCM + K;
    }
    }

    printf(“LCM(%d, %d, %d) = %d”, n1, n2, n3, LCM);
    }

    OUTPUT:
    12 15 10
    LCM(12, 15, 10) = 60


  • Trishna

    #include
    using namespace std;
    int gcd(int a,int b)
    {
    if(b==0)
    return a;
    return gcd(b,a%b);
    }

    int main() {
    // your code goes here
    int a,b,c,d,e;
    cin>>a;
    cin>>b;
    cin>>c;
    d=(a*b)/gcd(a,b);
    e=(d*c)/gcd(d,c);
    cout<<e;
    return 0;
    }


  • Alok

    LCM of 3 numbers

    #include
    using namespace std;

    int main() {
    int n, m, l;
    cin>>n>>m>>l;

    if(m>n)
    {
    m=m+n;
    n=m-n;
    m=m-n;
    }
    if(l>n)
    {
    l=l+n;
    n=l-n;
    l=l-n;
    }

    int add=n;

    while(n%m!=0 || n%l!=0)
    {
    n+=add;
    }

    cout<<n;
    return 0;
    }


  • Shubham

    def GCD(a, b):

    if b == 0:
    return a

    else:
    return GCD(b, a % b)

    def LCM(n1, n2):

    lcm = (n1*n2)//GCD(n1, n2)

    return lcm

    n1, n2, n3 = [int(input()) for x in range(3)]

    lcm_3 = LCM(n1, LCM(n2, n3)) // GCD(n1, GCD(n2, n3))

    print(“LCM OF 3 NOS IS = “, lcm_3)

    # DO try this ONE


    • Shubham

      def GCD(a, b):

      if b == 0:
      return a

      else:
      return GCD(b, a % b)

      def LCM(n1, n2):

      lcm = (n1*n2)//GCD(n1, n2)

      return lcm

      # print(GCD(60, 48))

      n1, n2, n3 = [int(input()) for x in range(3)]

      lcm_3 = LCM(n1, LCM(n2, n3)) // GCD(n1, GCD(n2, n3))

      print(“LCM OF 3 NOS IS = “, lcm_3)