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


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


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

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


  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // Function declaration
  5. int Findlcm(int x, int y);
  6.  
  7.  
  8. int main()
  9. {
  10. int num1, num2, LCM;
  11.  
  12. // Inputting two numbers from user
  13. cout<<"Enter any 2 numbers to find LCM: "<<endl;
  14. cin>>num1;
  15. cin>>num2;
  16.  
  17. if(num1 > num2)
  18. LCM = Findlcm(num2, num1);
  19. else
  20. LCM = Findlcm(num1, num2);
  21.  
  22. cout<<"LCM of "<<num1 << " and "<< num2 <<" is: "<<LCM;
  23.  
  24. return 0;
  25. }
  26.  
  27. int Findlcm(int x, int y)
  28. {
  29. static int multiple = 0;
  30.  
  31. // Increments multiple by adding max value to it
  32. multiple += y;
  33.  
  34. if((multiple % x == 0) && (multiple % y == 0))
  35. {
  36. return multiple;
  37. }
  38. else
  39. {
  40. return Findlcm(x, y);
  41. }
  42. }

Result

C++ language techstudy.org

     





Related Topics




Popular Posts