Dec 13, 2019, 03:03 AM

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

Started by thiruvasagamani, Sep 22, 2008, 05:48 PM

Go Down

#### thiruvasagamani ##### Sep 22, 2008, 05:48 PM
Find G.C.D and L.C.M of two numbers

Code: [Select]
`#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 variableelsen= 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);

#### Sam Opoka #1
##### Jan 24, 2012, 04:03 PM

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

Code: [Select]
`#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 variableelsen= 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 #2
##### Apr 02, 2012, 12:27 AM
#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

Go Up

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:
Verification: 