Skip to content
Related Articles

Related Articles

Sum of the products of same placed digits of two numbers
  • Last Updated : 01 Mar, 2021

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




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

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));
    }
}

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

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

Javascript




<script>
 
// Javascript program to calculate the
// sum of same placed digits
// of two numbers
 
function sumOfProductOfDigits(n1, n2)
{
    let sum = 0;
     
    // Loop until one of the numbers
    // have no digits remaining
    while (n1 > 0 && n2 > 0)
    {
        sum += ((n1 % 10) * (n2 % 10));
        n1 = Math.floor(n1/10);
        n2 = Math.floor(n2/10);
    }
    return sum;
}
 
// Driver Code
 
    let n1 = 25;
    let n2 = 1548;
 
    document.write(sumOfProductOfDigits(n1, n2));
 
// This code is contributed by Mayank Tyagi
 
</script>
Output: 
48

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :