Sep 24, 2019, 07:35 AM

News:

Choose a design and let our professionals help you build a successful website   - ITAcumens


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

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

previous topic - next topic
Go Down

thiruvasagamani

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 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


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 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

Go Up
 

Quick Reply

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:
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview
IT Acumens Web Design Chennai | GinGly :: Mobile SMS Backup | CineBuzz :: Latest Cinema News | My Kids Diary :: Kids magical moment :: Book domain name @ 99 Rs monthly
Copyright 2005 - 2019 :: IT Acumens :: All Rights Reserved.
ITAcumens Forum with 2 lakhs post running for 14 years - Powered by IT Acumens Dedicated Server