C Program to Find LCM of a Number using Recursion

The following C program, using recursion, finds the LCM. An LCM is the lowest common multiple of any 2 numbers.

Here is the source code of the C program to find LCM of a Number using Recursion. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Find LCM of a Number using Recursion
  3.  */
  4. #include <stdio.h>
  5.  
  6. int lcm(int, int);
  7.  
  8. int main()
  9. {
  10.     int a, b, result;
  11.     int prime[100];
  12.  
  13.     printf("Enter two numbers: ");
  14.     scanf("%d%d", &a, &b);
  15.     result = lcm(a, b);
  16.     printf("The LCM of %d and %d is %d\n", a, b, result);
  17.     return 0;
  18. }
  19.  
  20. int lcm(int a, int b)
  21. { 
  22.     static int common = 1;
  23.  
  24.     if (common % a == 0 && common % b == 0)
  25.     {
  26.         return common;
  27.     }
  28.     common++;
  29.     lcm(a, b);
  30.     return common;
  31. }


$ cc pgm22.c
$ a.out
Enter two numbers: 456
12
The LCM of 456 and 12 is 456
 
$ a.out
Enter two numbers: 45 75
The LCM of 45 and 75 is 225

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.
Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.

Subscribe Newsletter & Posts
Follow Manish & Sanfoundry