Friday, November 23, 2012

WRITE A FUNCTION TO THE GCD OF TWO NUMBERS. USING GCD FUNCTION,WRITE A MAIN PROGRAM TO FIND THE LCM OF FOUR NUMBERS.


/* WRITE A FUNCTION TO THE GCD OF TWO NUMBERS. USING GCD
     FUNCTION,WRITE A MAIN PROGRAM TO FIND THE LCM OF FOUR 
     NUMBERS.         
*/
#include<stdio.h>
#include<conio.h>

int gcd(int x,int y)
{
            int temp,rem;
            if(x>y)
            {
                        temp=x;
                        x=y;
                        y=temp;
            }
            while(rem!=0)
            {
                        rem=y%x, y=x, x=rem;
            }
            return y;
}

void main()
{
            int a,b,c,d;
            int lcm1,lcm2,lcm;
            clrscr();
            printf("Enter four numbers : ");
            scanf("%d %d %d %d",&a,&b,&c,&d);
            lcm1=(a*b)/gcd(a,b);
            lcm2=(c*d)/gcd(c,d);
            lcm=(lcm1*lcm2)/gcd(lcm1,lcm2);
            printf("The lcm of %d,%d,%d,%d is : %d ",a,b,c,d,lcm);
            getch();
}






/*
    LOGIC :
                               (a*b)
   -> LCM(a,b) = ------------
                            GCD(a,b)
                                                                 LCM(a,b) * LCM(c,d)
   -> LCM of four numbers(a,b,c,d) =  -----------------------------------
                                                              GCD( LCM(a,b),LCM(c,d) )
*/

/*
            =====OUT PUT=====

            Enter four numbers : 2 4 6 8
            The lcm of 2,4,6,8 is : 24
*/

0 comments:

Post a Comment