Program to compute GCD &LCM


    # include <iostream.h>
    # include<conio.h>
    void swap(int &, int &);
    long LCM (long, long) ;
    long GCD(long, long);
    void main( )
    {
        int m,n;
        cout<<"\n Enter two positive integers : ";
        cin >> m >> n ;
        cout<<"GCD ( " << m <<", "<<n<<" ) = " <<GCD(m,n)<<endl;
        cout<<"LCM ( " << m <<", "<<n<<" ) = " <<LCM(m,n)<<endl;
    }
    void swap(int & x, int & y)
    {
         //exchange the values of x and y
            int temp = x;
            x = y;
            y = temp;
    }
        long GCD(long m, long n)
          {
            if(m<n)
            swap(m,n);
            while (n > 0)
            {
                int r = m % n;
                m = n;
                n = r ;
            }
            return m;
        }
    long LCM(long m, long n)
    {
    return m * n/GCD(m, n);
    }

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|