Sunday, December 30, 2012

Find LCM In C : Programming

Find LCM In C : Programming:
LCM ( Least Common Multiple ) is a multiple two or more numbers , their multiple smallest ( non zero ) number is called  LCM.


Example :
  • Assume we have two number 4 and 5.
  • Multiples two number 4 and 5 separately.
  • Multiples of 4 : 4,8,16,20,24
  • Multiples of 5 : 5,10,15,20,25
  • 20 is Least Common Multiple. 


Program :

#include<conio.h>
#include<stdio.h>
int lcm(int,int);
void main()
{

int x,y,s,b,l;
char ch;
    do{
        clrscr();
        printf("\n Enter 1st no:");
        scanf("%d",&x);
        printf("\n Enter 2nd no:");
        scanf("%d",&y);

        if(x>y)
        {
            b=x;
            s=y;
        }
        else
        {
            b=y;
            s=x;
        }
        l=lcm(b,s);
    printf("\n <%d> and <%d> LCM value : %d ",b,s,l);
    printf("\n Continue (Y/N):");
    ch=getch();
    }while(ch=='Y'||ch=='y');
}


int lcm(int b,int s)
{

int c=1,i=2;
    while(i<=b/2)
    {

        if(s%i==0 && b%i==0)
        {
           s=s/i;
           b=b/i;
           c=c*i;
          i=2;
        }
    i++;
    }
    c=c*s*b;
    return c;
}

Output :





If you have any Further Information and Quires about this Article don't hesitate to comment in comment box below.
                                                           AND

 If you like this article , like our Facebook like page and click  +1 to support 123techguide

No comments:

Post a Comment