Sum and Product of digits in a number that divide the number Last Updated : 25 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Given a positive integer N. The task is to find the sum and product of digits of the number which evenly divides the number n. Examples: Input: N = 12 Output: Sum = 3, product = 2 1 and 2 divide 12. So, their sum is 3 and product is 2. Input: N = 1012 Output: Sum = 4, product = 2 1, 1 and 2 divide 1012. Approach: The idea is to find each digit of the number n by modulus 10 and then check whether it divides n or not. Accordingly, add it to the sum and multiply it with the product. Notice that the digit can be 0, so take care of that case. Below is the implementation of the above approach: C++ // C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Print the sum and product of digits // that divides the number. void countDigit(int n) { int temp = n, sum = 0, product = 1; while (temp != 0) { // Fetching each digit of the number int d = temp % 10; temp /= 10; // Checking if digit is greater than 0 // and can divides n. if (d > 0 && n % d == 0) { sum += d; product *= d; } } cout << "Sum = " << sum; cout << "\nProduct = " << product; } // Driver code int main() { int n = 1012; countDigit(n); return 0; } Java // Java implementation of the // above approach import java.lang.*; import java.util.*; class GFG { // Print the sum and product of // digits that divides the number. static void countDigit(int n) { int temp = n, sum = 0, product = 1; while (temp != 0) { // Fetching each digit of // the number int d = temp % 10; temp /= 10; // Checking if digit is greater // than 0 and can divides n. if (d > 0 && n % d == 0) { sum += d; product *= d; } } System.out.print("Sum = " + sum); System.out.print("\nProduct = " + product); } // Driver code public static void main(String args[]) { int n = 1012; countDigit(n); } } // This code is contributed // by Akanksha Rai(Abby_akku) Python3 # Python3 implementation of the above approach # Print the sum and product of digits # that divides the number. def countDigit(n): temp = n sum = 0 product = 1 while(temp != 0): # Fetching each digit of the number d = temp % 10 temp //= 10 # Checking if digit is greater # than 0 and can divides n. if(d > 0 and n % d == 0): sum += d product *= d print("Sum =", sum) print("Product =", product) # Driver code if __name__=='__main__': n = 1012 countDigit(n) # This code is contributed # by Kirti_Mangal C# // C# implementation of the // above approach using System; class GFG { // Print the sum and product of // digits that divides the number. static void countDigit(int n) { int temp = n, sum = 0, product = 1; while (temp != 0) { // Fetching each digit of // the number int d = temp % 10; temp /= 10; // Checking if digit is greater // than 0 and can divides n. if (d > 0 && n % d == 0) { sum += d; product *= d; } } Console.Write("Sum = " + sum); Console.Write("\nProduct = " + product); } // Driver code public static void Main() { int n = 1012; countDigit(n); } } // This code is contributed // by Akanksha Rai(Abby_akku) PHP <?php // PHP implementation of the above approach // Print the sum and product of digits // that divides the number. function countDigit($n) { $temp = $n; $sum = 0; $product = 1; while ($temp != 0) { // Fetching each digit of the number $d = $temp % 10; $temp =(int)($temp/10); // Checking if digit is greater than 0 // and can divides n. if ($d > 0 && $n % $d == 0) { $sum += $d; $product *= $d; } } echo "Sum = ".$sum; echo "\nProduct = ".$product; } // Driver code $n = 1012; countDigit($n); // This code is contributed by mits ?> JavaScript <script> // java script implementation of the above approach // Print the sum and product of digits // that divides the number. function countDigit(n) { let temp = n; let sum = 0; let product = 1; while (temp != 0) { // Fetching each digit of the number let d = temp % 10; temp =parseInt(temp/10); // Checking if digit is greater than 0 // and can divides n. if (d > 0 && n % d == 0) { sum += d; product *= d; } } document.write( "Sum = "+sum); document.write( "<br>Product = "+product); } // Driver code let n = 1012; countDigit(n); // This code is contributed by Gottumukkala Bobby </script> Output: Sum = 4 Product = 2 Time Complexity: O(log10n), Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check if the sum of digits of a number divides it S Shivam.Pradhan Follow Improve Article Tags : Misc Mathematical DSA number-digits Number Divisibility +1 More Practice Tags : MathematicalMisc Similar Reads Find count of digits in a number that divide the number Given a positive integer n. The task is to find count of digits of number which evenly divides the number n.Examples: Input : n = 12 Output : 2 1 and 2 divide 12. Input : n = 1012 Output : 3 1, 1 and 2 divide 1012. Recommended PracticeCount DigitsTry It! The idea is to find each digit of the number 4 min read Number of digits in the product of two numbers Given two integers a and b. The problem is to find the number of digits in the product of these two integers.Examples: Input : a = 12, b = 4 Output : 2 12 * 4 = 48 (2 digits) Input : a = 33, b = -24 Output : 3 33 * -24 = -792 (3 digits)Recommended PracticeProduct SumTry It! Basic Approach: Multiply 8 min read Check if the sum of digits of a number divides it Given a number n, the task is to check if the sum of digits of the given number divides the number or not. Examples: Input : n = 12Output : YesExplanation: Sum of digits = 1+2 =3 and 3 divides 12.Input : n = 15Output : NoExplanation: Sum of digits = 1+5 =6 and 15 % 6 != 0.Using Iterative Method - O( 6 min read Check if the sum of digits of a number divides it Given a number n, the task is to check if the sum of digits of the given number divides the number or not. Examples: Input : n = 12Output : YesExplanation: Sum of digits = 1+2 =3 and 3 divides 12.Input : n = 15Output : NoExplanation: Sum of digits = 1+5 =6 and 15 % 6 != 0.Using Iterative Method - O( 6 min read Find the number in a range having maximum product of the digits Given a range represented by two positive integers L and R. Find the number lying in the range having the maximum product of the digits.Examples: Input : L = 1, R = 10 Output : 9 Input : L = 51, R = 62 Output : 59 Approach : The key idea here is to iterate over the digits of the number R starting fr 15+ min read Find the number in a range having maximum product of the digits Given a range represented by two positive integers L and R. Find the number lying in the range having the maximum product of the digits.Examples: Input : L = 1, R = 10 Output : 9 Input : L = 51, R = 62 Output : 59 Approach : The key idea here is to iterate over the digits of the number R starting fr 15+ min read Like