Write C program to find LCM of two numbers using recursion


Introduction

I have used Code::blocks 12 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

Write C program to find LCM of two numbers using recursion


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

Result

C language

     





Related Topics




Popular Posts