Given a number n, find GCD of its digits.
Input : 345 Output : 1 GCD of 3, 4 and 5 is 1. Input : 2448 Output : 2 GCD of 2, 4, 4 and 8 is 2
We traverse the digits of number one by one using below loop
digit = n mod 10; n = n / 10;
While traversing digits, we keep track of current GCD and keep updating GCD by finding GCD of current digit with current GCD.
This article is contributed by Dibyendu Roy Chaudhuri. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
- Count pairs of natural numbers with GCD equal to given number
- Given GCD G and LCM L, find number of possible pairs (a, b)
- Find M such that GCD of M and given number N is maximum
- GCD of a number raised to some power and another number
- Sum of digits of a given number to a given power
- Even digits Sum and Odd digits sum divisible by 4 and 3 respectively
- Round-off a number to a given number of significant digits
- Immediate smallest number after re-arranging the digits of a given number
- Find smallest number formed by inverting digits of given number N
- Smallest Subarray with given GCD
- Queries for GCD of all numbers of an array except elements in a given range
- GCD of elements in a given range
- Array with GCD of any of its subset belongs to the given array
- Find the GCD that lies in given range
- Pairs with GCD equal to one in the given range
- Find any pair with given GCD and LCM
- Find the GCD of N Fibonacci Numbers with given Indices
- Check if there is any pair in a given range with GCD is divisible by k
- Find two numbers whose sum and GCD are given
- Maximum GCD of N integers with given product