Click To Get Off-Campus Placement Jobs Info !!!
Capgemini Recruitment Process 2020 – Updated Test Pattern and Selection Process in Details Latest Pattern !!
Program Discussion :: Basics
Home > Programs > Basics

76 / 244

Write a program to find the LCM and GCD.

Answer:

#include <stdio.h>
 #include <iostream>
using namespace std;
void main()
{
    int num1, num2, gcd, lcm, remainder, numerator, denominator;
   cout<<"Enter two numbers\n";
    cin>> num1>>num2;
    if (num1 > num2)
    {
        numerator = num1;
        denominator = num2;
    }
    else
    {
        numerator = num2;
        denominator = num1;
    }
    remainder = numerator % denominator;
    while (remainder != 0)
    {
        numerator   = denominator;
        denominator = remainder;
        remainder   = numerator % denominator;
    }
    gcd = denominator;
    lcm = num1 * num2 / gcd;
    cout<<"GCD of "<< num1<<" and "<< num2<<" is "<< gcd;
    cout<<"LCM of "<< num1<<" and "<< num2<<" is "<< lcm;
}

Asked In :: PEOL

Post Your Answer Here:

Language:

Post Your Reply Here:



Language:

Post Your Reply Here:



Language:

Post Your Reply Here:



Language:

Post Your Reply Here: