Given a positive integer N. The task is to find 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 the 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 ?> |
Sum = 4 Product = 2
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:
- Find count of digits in a number that divide the number
- Count digits in given number N which divide N
- Check if all digits of a number divide it
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Divide a number into two parts such that sum of digits is maximum
- Number of digits in the product of two numbers
- Program to calculate product of digits of a number
- Smallest number k such that the product of digits of k is equal to n
- Maximum sum and product of the M consecutive digits in a number
- Find the number in a range having maximum product of the digits
- Check if product of digits of a number at even and odd places is equal
- Check whether product of digits at even places of a number is divisible by K
- Maximum of sum and product of digits until number is reduced to a single digit
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Find the number of ways to divide number into four parts such that a = c and b = d
- Count number of ways to divide a number in 4 parts
- 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
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

