LCM recursion || C++
In this, we are going to see a program to find LCM using recursion in C++ Programming Language.



The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <conio.h>
    
    int Findlcm(int x, int y);
     
    void main()
    {
        int num1, num2, LCM;
     
        
        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;
        getch();
        clrscr();
    }
     
    int Findlcm(int x, int y)
    {
        static int multiple = 0;
     
        
        multiple += y;
     
        if(multiple % x == 0)
        {
            return multiple;
        }
        else
        {
            return Findlcm(x, y);
        }
    }

The Code given below can be used in gcc/g++ Compilers: -

#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);
        }
    }

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post