Sum of the products of same placed digits of two numbers

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 in N2. As N1 has 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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.




My Personal Notes arrow_drop_up

External Technical Content Reviewer at GeeksforGeeks

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.