Results 1 to 4 of 4

Thread: Find L.C.M. (Least common multiple) of two numbers in C

  1. #1
    MorganCooper is offline Senior Member
    Join Date
    Dec 2009
    Posts
    233
    Rep Power
    3

    Default Find L.C.M. (Least common multiple) of two numbers in C

    Hi. I am learning C programming language. Right now I have little bit knowledge of C. I am little bit confuses between code for L.C.M. (Least common multiple) of two numbers. Please give me some accurate code for LCM of two numbers. Thanks in advanced.

  2. #2
    BellWard is offline Senior Member
    Join Date
    Dec 2009
    Posts
    236
    Rep Power
    3

    Default

    Refer following program to find L.C.M. of two numbers in C

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int find(int A, int B){
    if (A<B && A==0){
    return B;
    }
    if (B<A && B==0){
    return A;
    }
    if (B>A){
    return find(A,B%A);
    }
    if (A>B){
    return find(B,A%B);
    }
    }
    int main(void){
    int a=9,b=6;
    printf("%dn",(a*b)/find(a,b));
    system("pause");
    return 0;
    }
    Output:
    3

    ***)
    I have tried the given code but it didn’t solve my problem. Please try to give me some other solution.

  3. #3
    CoxWatson is offline Senior Member
    Join Date
    Dec 2009
    Posts
    232
    Rep Power
    3

    Default

    Try the given code which is helpful for you. But the given code id in JAVA

    Code:
    public class FindLCM {
            public static int determineLCM(int x, int y) {
                    int a, b;
                    if (x > y) {
                            a = x;
                            b = y;
                    } else {
                            a = x;
                            b = y;
                    }
                    for (int x = 1; x <= b; x++) {
                            if ((a * x) % b == 0) {
                                    return x * a;
                            }
                    }
                    throw new Error("Error");
            }
    
            public static void main(String[] args) {
                    FindLCM lcm = new FindLCM();
                    int d = lcm.determineLCM(6, 7);
                    System.out.println(d);
    
            }
    }

  4. #4
    nsnabhi is offline Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default

    int lcmx(int x,int y)
    {
    static int lcm = 1 ;
    if(x == y)
    return x*lcm;


    int fact = 2 ;
    if(x == 1 && y == 1)
    return lcm ;
    while(((x% fact != 0) && (y% fact != 0)) && (fact <= x || fact <= y) )
    {

    fact++;

    }
    if(x%fact == 0 && y%fact == 0 )
    {
    x = x/fact;
    y = y/fact;
    lcm = lcm*fact;

    }
    else if(x%fact == 0)
    {
    x = x/fact ;
    lcm = lcm*fact;


    }
    else if(y%fact == 0)
    {

    y = y/fact ;
    lcm = lcm*fact ;

    }
    return lcmx(x,y);

    }

Similar Threads

  1. AMD again with red numbers
    By Lolita Jovina in forum Latest Hardware News
    Replies: 0
    Last Post: 10-16-2010, 06:50 AM
  2. common LAN technology?
    By AlReyes in forum Wireless Networking
    Replies: 2
    Last Post: 01-02-2010, 06:50 AM
  3. common LAN technology?
    By saul_turner in forum Applications
    Replies: 1
    Last Post: 12-18-2009, 11:00 PM
  4. Blu-Ray soon will become more common
    By John Emburey in forum Latest Hardware News
    Replies: 0
    Last Post: 11-23-2009, 06:56 AM
  5. Microsoft, Linux Find Common Cause
    By Jadarius Hugue in forum Latest Hardware News
    Replies: 0
    Last Post: 07-31-2009, 09:36 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48