Open In App

Sum of the products of same placed digits of two numbers

Improve
Improve
Like Article
Like
Save
Share
Report

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:

Approach to solve this problem would be to convert the two given numbers to strings and then iterate over each character of both strings. For each character, we can multiply the corresponding digits of the two numbers and add it to the sum. We need to also take care of the case where the two numbers have different lengths. In this case, we can assume that the smaller number has leading zeros.

Below is the implementation of the above approach: 

C++




#include <bits/stdc++.h>
using namespace std;
 
int sumOfProductOfDigits(int n1, int n2)
{
    string s1 = to_string(n1);
    string s2 = to_string(n2);
 
    int sum = 0, len1 = s1.length(), len2 = s2.length();
 
    // Pad the smaller string with leading zeros
    if (len1 < len2) {
        s1.insert(0, len2 - len1, '0');
        len1 = len2;
    }
    else if (len2 < len1) {
        s2.insert(0, len1 - len2, '0');
        len2 = len1;
    }
 
    // Iterate over each character and multiply corresponding digits
    for (int i = 0; i < len1; i++) {
        int digit1 = s1[i] - '0';
        int digit2 = s2[i] - '0';
        sum += digit1 * digit2;
    }
 
    return sum;
}
 
int main()
{
    int n1 = 25;
    int n2 = 1548;
 
    cout << sumOfProductOfDigits(n1, n2);
 
    return 0;
}


Java




import java.util.*;
 
public class Main {
  static int sumOfProductOfDigits(int n1, int n2)
  {
    String s1 = Integer.toString(n1);
    String s2 = Integer.toString(n2);
 
    int sum = 0;
    int len1 = s1.length();
    int len2 = s2.length();
 
    // Pad the smaller string with leading zeros
    if (len1 < len2) {
      s1 = String.format("%0" + len2 + "d", n1);
      len1 = len2;
    }
    else if (len2 < len1) {
      s2 = String.format("%0" + len1 + "d", n2);
      len2 = len1;
    }
 
    // Iterate over each character and multiply
    // corresponding digits
    for (int i = 0; i < len1; i++) {
      int digit1
        = Character.getNumericValue(s1.charAt(i));
      int digit2
        = Character.getNumericValue(s2.charAt(i));
      sum += digit1 * digit2;
    }
 
    return sum;
  }
 
  public static void main(String[] args)
  {
    int n1 = 25;
    int n2 = 1548;
    System.out.println(sumOfProductOfDigits(n1, n2));
  }
}


Python3




def sum_of_product_of_digits(n1, n2):
    s1 = str(n1)
    s2 = str(n2)
 
    sum = 0
    len1 = len(s1)
    len2 = len(s2)
 
    # Pad the smaller string with leading zeros
    if len1 < len2:
        s1 = s1.zfill(len2)
        len1 = len2
    elif len2 < len1:
        s2 = s2.zfill(len1)
        len2 = len1
 
    # Iterate over each character and multiply corresponding digits
    for i in range(len1):
        digit1 = int(s1[i])
        digit2 = int(s2[i])
        sum += digit1 * digit2
 
    return sum
 
n1 = 25
n2 = 1548
print(sum_of_product_of_digits(n1, n2))


C#




using System;
 
public class GFG
{
    public static int SumOfProductOfDigits(int n1, int n2)
    {
        string s1 = n1.ToString();
        string s2 = n2.ToString();
 
        int sum = 0;
        int len1 = s1.Length;
        int len2 = s2.Length;
 
        // Pad the smaller string with leading zeros
        if (len1 < len2)
        {
            s1 = s1.PadLeft(len2, '0');
            len1 = len2;
        }
        else if (len2 < len1)
        {
            s2 = s2.PadLeft(len1, '0');
            len2 = len1;
        }
 
        // Iterate over each character and multiply corresponding digits
        for (int i = 0; i < len1; i++)
        {
            int digit1 = int.Parse(s1[i].ToString());
            int digit2 = int.Parse(s2[i].ToString());
            sum += digit1 * digit2;
        }
 
        return sum;
    }
 
    public static void Main()
    {
        int n1 = 25;
        int n2 = 1548;
 
        Console.WriteLine(SumOfProductOfDigits(n1, n2));
    }
}


Javascript




function sumOfProductOfDigits(n1, n2) {
    let s1 = n1.toString();
    let s2 = n2.toString();
 
    let sum = 0;
    let len1 = s1.length;
    let len2 = s2.length;
 
    // Pad the smaller string with leading zeros
    if (len1 < len2) {
        s1 = '0'.repeat(len2 - len1) + s1;
        len1 = len2;
    } else if (len2 < len1) {
        s2 = '0'.repeat(len1 - len2) + s2;
        len2 = len1;
    }
 
    // Iterate over each character and multiply corresponding digits
    for (let i = 0; i < len1; i++) {
        const digit1 = parseInt(s1[i]);
        const digit2 = parseInt(s2[i]);
        sum += digit1 * digit2;
    }
 
    return sum;
}
 
const n1 = 25;
const n2 = 1548;
 
console.log(sumOfProductOfDigits(n1, n2));


Output

48




Time Complexity: O(N)
Auxiliary Space: O(1)

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




Time Complexity: O(min(log10n1, log10n2))
Auxiliary Space: O(1) as no extra space has been used.



Last Updated : 31 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads