Write C++ program to find LCM of two numbers


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.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int i, num1, num2, max, cm=1;
  8.  
  9. //Read two numbers from user
  10. cout<<"Enter any two numbers:";
  11. cin>>num1;
  12. cin>>num2;
  13.  
  14. // Find max number between num1 and num2
  15. max = (num1 > num2) ? num1 : num2;
  16.  
  17. i = max;
  18.  
  19. //Loop runs forever till lcm is not found
  20. while(1)
  21. {
  22. if(i%num1 == 0 && i%num2 == 0)
  23. {
  24. //If i divides both num1 and num2 then lcm is found hence exit from loop
  25. cm = i;
  26. break;
  27. }
  28. //If lcm is not found then generate next multiple of max between both numbers
  29.  
  30. i += max;
  31. }
  32.  
  33. cout<<"LCM of " << num1 << " and " << " is "<< cm;
  34.  
  35. return 0;
  36.  
  37. }
  38.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts