Sum of the products of same placed digits of two numbers - GeeksforGeeks

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++
Java
Python3
C#
Javascript
// 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 Codeint main(){    int n1 = 25;    int n2 = 1548;     cout << sumOfProductOfDigits(n1, n2);} // This code is contributed by grand_master
// 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 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 Coden1 = 25;n2 = 1548; print(sumOfProductOfDigits(n1, n2)); # This code is contributed by Nidhi_biet
// C# program to calculate the// sum of same placed digits of// two numbersusing System;class GFG{ // Function to find the sum of the// products of their corresponding digitsstatic 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 Codepublic static void Main(){    int n1 = 25;    int n2 = 1548;     Console.WriteLine(            sumOfProductOfDigits(n1, n2));}} // This code is contributed by 29AjayKumar
<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>
48
 
grand_master
29AjayKumar
nidhi_biet
mayanktyagi1709
number-digits
Mathematical
Recursion
School Programming
Mathematical
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Algorithm to solve Rubik's Cube
Program to convert a given number to words
Program to multiply two matrices
Modular multiplicative inverse
Program to print prime numbers from 1 to N.
Recursion
Program for Tower of Hanoi
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Program for Sum of the digits of a given number
Backtracking | Introduction