Q:

Write C++ program to find LCM of two numbers using recursion

0

Write C++ program to find LCM of two numbers using recursion

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
using namespace std;
 
// Function declaration
int Findlcm(int x, int y);
 
 
int main()
{
    int num1, num2, LCM;
 
    // Inputting two numbers from user
    cout<<"Enter any 2 numbers to find LCM: "<<endl;
    cin>>num1;
    cin>>num2;
 
    if(num1 > num2)
        LCM = Findlcm(num2, num1);
    else
        LCM = Findlcm(num1, num2);
 
    cout<<"LCM of "<<num1 << " and "<< num2 <<" is: "<<LCM;
 
    return 0;
}
 
int Findlcm(int x, int y)
{
    static int multiple = 0;
 
    // Increments multiple by adding max value to it
    multiple += y;
 
    if((multiple % x == 0) && (multiple % y == 0))
    {
        return multiple;
    }
    else
    {
        return Findlcm(x, y);
    }
}

Result:

Enter any 2 numbers to find LCM: 

20

50

LCM of 20 and 50 is: 100

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write C++ program to find HCF of two numbers using... >>
<< Write C++ program to find reverse of a number usin...