Given two positive integers** N1 and N2**, the task is to find the sum of the products of the same placed digits of the two numbers.

**Note:** For numbers of unequal length, the preceding digits of the smaller number needs to be treated as 0.

**Examples:**

Input:N1 = 5, N2 = 67

Output:35

Explanation:

At one’s place, we have digits 5 and 7, their product is 35. At ten’s place we have 6 inN2. AsN1has no digit at ten’s place, 6 will be multiplied with 0, leading to no effect on the sum. Hence, the calculated sum is 35.

Input:N1 = 25, N2 = 1548

Output:48

Explanation:

Sum = 5 * 8 + 2 * 4 + 0 * 5 + 0 * 1 = 48.

**Approach:**

To solve the problem mentioned above, we need to follow the steps below:

- Extract the rightmost digits of the two numbers and multiply them and add their product to
**sum**. - Now remove the digit.
- Keep repeating the above two steps until one of them is reduced to 0. Then, print the final value of
**sum**calculated.

Below is the implementation of the above approach:

## C++

`// C++ program to calculate the ` `// sum of same placed digits ` `// of two numbers ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `sumOfProductOfDigits(` `int` `n1, ` `int` `n2) ` `{ ` ` ` `int` `sum = 0; ` ` ` ` ` `// Loop until one of the numbers ` ` ` `// have no digits remaining ` ` ` `while` `(n1 > 0 && n2 > 0) ` ` ` `{ ` ` ` `sum += ((n1 % 10) * (n2 % 10)); ` ` ` `n1 /= 10; ` ` ` `n2 /= 10; ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n1 = 25; ` ` ` `int` `n2 = 1548; ` ` ` ` ` `cout << sumOfProductOfDigits(n1, n2); ` `} ` ` ` `// This code is contributed by grand_master ` |

*chevron_right*

*filter_none*

## Java

`// Java program to calculate the ` `// sum of same placed digits of ` `// two numbers ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find the sum of the ` ` ` `// products of their corresponding digits ` ` ` `static` `int` `sumOfProductOfDigits(` `int` `n1, ` ` ` `int` `n2) ` ` ` `{ ` ` ` `int` `sum = ` `0` `; ` ` ` `// Loop until one of the numbers ` ` ` `// have no digits remaining ` ` ` `while` `(n1 > ` `0` `&& n2 > ` `0` `) { ` ` ` `sum += ((n1 % ` `10` `) * (n2 % ` `10` `)); ` ` ` `n1 /= ` `10` `; ` ` ` `n2 /= ` `10` `; ` ` ` `} ` ` ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` ` ` `int` `n1 = ` `25` `; ` ` ` `int` `n2 = ` `1548` `; ` ` ` ` ` `System.out.println( ` ` ` `sumOfProductOfDigits(n1, n2)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to calculate the ` `# sum of same placed digits ` `# of two numbers ` ` ` `def` `sumOfProductOfDigits(n1, n2): ` ` ` ` ` `sum1 ` `=` `0` `; ` ` ` ` ` `# Loop until one of the numbers ` ` ` `# have no digits remaining ` ` ` `while` `(n1 > ` `0` `and` `n2 > ` `0` `): ` ` ` ` ` `sum1 ` `+` `=` `((n1 ` `%` `10` `) ` `*` `(n2 ` `%` `10` `)); ` ` ` `n1 ` `=` `n1 ` `/` `/` `10` `; ` ` ` `n2 ` `=` `n2 ` `/` `/` `10` `; ` ` ` ` ` `return` `sum1; ` ` ` `# Driver Code ` `n1 ` `=` `25` `; ` `n2 ` `=` `1548` `; ` ` ` `print` `(sumOfProductOfDigits(n1, n2)); ` ` ` `# This code is contributed by Nidhi_biet ` |

*chevron_right*

*filter_none*

## C#

`// C# program to calculate the ` `// sum of same placed digits of ` `// two numbers ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to find the sum of the ` `// products of their corresponding digits ` `static` `int` `sumOfProductOfDigits(` `int` `n1, ` ` ` `int` `n2) ` `{ ` ` ` `int` `sum = 0; ` ` ` ` ` `// Loop until one of the numbers ` ` ` `// have no digits remaining ` ` ` `while` `(n1 > 0 && n2 > 0) ` ` ` `{ ` ` ` `sum += ((n1 % 10) * (n2 % 10)); ` ` ` `n1 /= 10; ` ` ` `n2 /= 10; ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n1 = 25; ` ` ` `int` `n2 = 1548; ` ` ` ` ` `Console.WriteLine( ` ` ` `sumOfProductOfDigits(n1, n2)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

48

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:

- Spy Number (Sum and Products of Digits are same)
- Minimize the Sum of all the subarrays made up of the products of same-indexed elements
- Minimum digits to be removed to make either all digits or alternating digits same
- Sum of all products of the Binomial Coefficients of two numbers up to K
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum number of digits to be removed so that no two consecutive digits are same
- Maximum Sum of Products of Two Arrays
- Maximize the sum of products of the degrees between any two vertices of the tree
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Maximum non-attacking Rooks that can be placed on an N*N Chessboard
- Check if all objects of type A and B can be placed on N shelves
- Count of smaller rectangles that can be placed inside a bigger rectangle
- Check if two numbers have same number of digits
- Sum of pairwise products
- Maximize sum of pairwise products generated from the given Arrays
- Sum of all ordered pair-products from a given array
- Count distinct pairs from two arrays having same sum of digits
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M

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.