Given a number n, find LCM of its digits.
Examples:
Input : 397 Output : 63 LCM of 3, 9 and 7 is 63. Input : 244 Output : 4 LCM of 2, 4 and 4 is 4.
We traverse the digits of number one by one below loop
digit = n mod 10;
n = n / 10;
While traversing digits, we keep track of current LCM and keep updating LCM by finding LCM of current digit with current LCM.
C++
// CPP program to find LCM of digits of a number #include<iostream> #include<boost/math/common_factor.hpp> using namespace std; int digitLCM(int n) { int lcm = 1; while (n > 0) { lcm = boost::math::lcm(n%10, lcm); // If at any point LCM become 0. // return it if (lcm == 0) return 0; n = n/10; } return lcm; } // driver code int main() { long n = 397; cout << digitLCM(n); return 0; } |
Java
// Java program to find LCM of digits of a number class GFG { // define lcm function static int lcm_fun(int a, int b) { if (b == 0) return a; return lcm_fun(b, a % b); } static int digitLCM(int n) { int lcm = 1; while (n > 0) { lcm = (n % 10 * lcm) / lcm_fun(n % 10, lcm); // If at any point LCM become 0. // return it if (lcm == 0) return 0; n = n/10; } return lcm; } // driver code public static void main(String[] args) { int n = 397; System.out.println(digitLCM(n)); } } // This code is contributed by mits |
Python3
# Python3 program to find # LCM of digits of a number # define lcm function def lcm_fun(a, b): if (b == 0): return a; return lcm_fun(b, a % b); def digitLCM(n): lcm = 1; while (n > 0): lcm = int((n % 10 * lcm) / lcm_fun(n % 10, lcm)); # If at any point LCM # become 0. return it if (lcm == 0): return 0; n = int(n / 10); return lcm; # Driver code n = 397; print(digitLCM(n)); # This code is contributed by mits |
C#
// C# program to find LCM of digits // of a number class GFG { // define lcm function static int lcm_fun(int a, int b) { if (b == 0) return a; return lcm_fun(b, a % b); } static int digitLCM(int n) { int lcm = 1; while (n > 0) { lcm = (n % 10 * lcm) / lcm_fun(n % 10, lcm); // If at any point LCM become 0. // return it if (lcm == 0) return 0; n = n/10; } return lcm; } // Driver Code public static void Main() { int n = 397; System.Console.WriteLine(digitLCM(n)); } } // This code is contributed by mits |
PHP
<?php // PHP program to find // LCM of digits of a number // define lcm function function lcm_fun($a, $b) { if ($b == 0) return $a; return lcm_fun($b, $a % $b); } function digitLCM($n) { $lcm = 1; while ($n > 0) { $lcm = (int)(($n % 10 * $lcm) / lcm_fun($n % 10, $lcm)); // If at any point LCM // become 0. return it if ($lcm == 0) return 0; $n = (int)($n / 10); } return $lcm; } // Driver code $n = 397; echo digitLCM($n); // This code is contributed by mits ?> |
Output:
63
This article is contributed by nikunj_agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Sum of LCM(1, n), LCM(2, n), LCM(3, n), ... , LCM(n, n)
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Find smallest number with given number of digits and sum of digits under given constraints
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Number of digits in the nth number made of given four digits
- Smallest number with given sum of digits and sum of square of digits
- Find the average of k digits from the beginning and l digits from the end of the given number
- Sum of the digits of square of the given number which has only 1's as its digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Given GCD G and LCM L, find number of possible pairs (a, b)
- Find the other number when LCM and HCF given
- Minimum number to be added to minimize LCM of two given numbers
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count numbers from given range having odd digits at odd places and even digits at even places
- Minimum number of digits to be removed so that no two consecutive digits are same
Improved By : Mithun Kumar

