Program to find G.C.D and L.C.M of two numbers

Started by thiruvasagamani, Sep 22, 2008, 12:18 PM

Previous topic - Next topic

thiruvasagamani

Find G.C.D and L.C.M of two numbers

#include <stdio.h>
#include <conio.h>

int gcd( int m, int n)
{
while( m!= n) // execute loop until m == n
{
if( m > n)
m= m - n; // large - small , store the results in large variable
else
n= n - m;
}
return ( m); // m or n is GCD
}

int lcm( int m, int n)
{
int gcd( int, int);
return( m * n / gcd (m , n)); // product of 2 numbers / gcd is lcm
}

main()
{
int m, n , lcm( int, int), gcd( int ,int);
printf(" enter anyu 2 values \n");
scanf("%d%d", &m,&n);
printf(" gcd is %d\n", gcd( m, n));
printf(" lcm is %d\n", lcm(m , n));
getch();
}

o/p:
If u enter m = 12 , n= 15 output gcd is 3 and output lcm is 60

NOTE 1:
gcd(12,15) = 3
gcd(12, 18) = 6
gcd(4 , 7) = 1

NOTE 2:

LCM of 12, 15 = 12 * 15 / gcd(12, 15);
Thiruvasakamani Karnan


Sam Opoka

Quote from: thiruvasagamani on Sep 22, 2008, 12:18 PM
Find G.C.D and L.C.M of two numbers

#include <stdio.h>
#include <conio.h>

int gcd( int m, int n)
{
while( m!= n) // execute loop until m == n
{
if( m > n)
m= m - n; // large - small , store the results in large variable
else
n= n - m;
}
return ( m); // m or n is GCD
}

int lcm( int m, int n)
{
int gcd( int, int);
return( m * n / gcd (m , n)); // product of 2 numbers / gcd is lcm
}

main()
{
int m, n , lcm( int, int), gcd( int ,int);
printf(" enter anyu 2 values \n");
scanf("%d%d", &m,&n);
printf(" gcd is %d\n", gcd( m, n));
printf(" lcm is %d\n", lcm(m , n));
getch();
}

o/p:
If u enter m = 12 , n= 15 output gcd is 3 and output lcm is 60

NOTE 1:
gcd(12,15) = 3
gcd(12, 18) = 6
gcd(4 , 7) = 1

NOTE 2:

LCM of 12, 15 = 12 * 15 / gcd(12, 15);


Elvin-Aze

#include<iostream>
#include<cstdlib>
long a,b,m,n,c;
using namespace std;
int main()
{cin>>m>>n;
a=m;
b=n;
while(n!=0)
{
c=m%n;
m=n;
n=c;}

   
    if(m!=1)
    cout<<a/m<<" "<<b/m<<endl;
    else
    cout<<a<<" "<<b<<endl;
   
    system("pause");
    return 0;
    }
use it will udnerstand best and quickest way