Given a number n, find LCM of its digits.
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# program to find LCM of digits
// of a number
// define lcm function
static int lcm_fun(int a, int b)
if (b == 0)
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)
n = n/10;
// Driver Code
public static void Main()
int n = 397;
// This code is contributed by mits
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 firstname.lastname@example.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.
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Minimum number of digits to be removed so that no two consecutive digits are same
- Find the Largest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits
- Smallest number with given sum of digits and sum of square of digits
- Number of digits in the nth number made of given four digits
- Find count of digits in a number that divide the number
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Number of digits to be removed to make a number divisible by 3
- Find the smallest number whose digits multiply to a given number n
- Build Lowest Number by Removing n digits from a given number
- Find maximum number that can be formed using digits of a given number
- Count number of digits after decimal on dividing a number
- Print a number strictly less than a given number such that all its digits are distinct.