How to find LCM of two Numbers in C ?

Answer : The LCM of two numbers a and b is the smallest positive integer that is perfectly divisible…

To Find LCM of Two Numbers in C

  • The LCM of two numbers a and b is the smallest positive integer that is perfectly divisible by both a and b (without a remainder). For example: The LCM of 72 and 120 is 360.

Sample Code

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%20%0A%2F%2F%20Recursive%20function%20to%20return%20gcd%20of%20a%20and%20b%0Aint%20gcd(int%20a%2C%20int%20b)%0A%7B%0A%2F%2F%20Everything%20divides%200%0A%20%20%20%20if%20(a%20%3D%3D%200%20%7C%7C%20b%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20return%200%3B%0A%2F%2F%20Base%20case%0A%20%20%20%20if%20(a%20%3D%3D%20b)%0A%20%20%20%20%20%20%20%20return%20a%3B%0A%2F%2F%20a%20is%20greater%0A%20%20%20%20if%20(a%20%3E%20b)%0A%20%20%20%20%20%20%20%20return%20gcd(a-b%2C%20b)%3B%0A%20%20%20%20return%20gcd(a%2C%20b-a)%3B%0A%7D%0A%2F%2F%20Function%20to%20return%20LCM%20of%20two%20numbers%0Aint%20lcm(int%20a%2C%20int%20b)%0A%7B%0A%20%20%20%20return%20(a*b)%2Fgcd(a%2C%20b)%3B%0A%7D%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20a%20%3D%2010%2C%20b%20%3D%2028%3B%0A%20%20%20%20printf(%22LCM%20of%20%25d%20and%20%25d%20is%20%25d%20%22%2C%20a%2C%20b%2C%20lcm(a%2C%20b))%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”c” manual=”LCM%20of%2010%20and%2028%20is%20140″ message=”” highlight=”” provider=”manual”/]
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like