Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmNumber of digits in the product of two numbers

Number of digits in the product of two numbers

Given two integers a and b. The problem is to find the number of digits in the product of these two integers.
Examples: 

Input : a = 12, b = 4
Output : 2
12 * 4 = 48 (2 digits)

Input : a = 33, b = -24
Output : 3
33 * -24 = -792 (3 digits)
Recommended Practice

Basic Approach: Multiply the two numbers and then by using looping construct find the number of digits in the product. Take the absolute value of the product for finding the number of digits.
 

C++




// C++ implementation to count number of digits
// in the product of two numbers
#include <bits/stdc++.h>
 
using namespace std;
 
// function to count number of digits
// in the product of two numbers
int countDigits(int a, int b){
   
    int count = 0;   
     
    // absolute value of the
    // product of two numbers
    int p = abs(a*b);
     
    // if product is 0
    if (p == 0) {  
        return 1;
    }
  
    // count number of digits in the product 'p'   
    while (p > 0){
        count++;
        p = p / 10;
    }
     
    // required count of digits   
    return count;
}
 
// Driver program to test above
int main()
{
    int a = 33;
    int b = -24;
      // Function call
    cout << "Number of digits = "
         << countDigits(a,b);
    return 0;
}


Java




// Java implementation to count
// number of digits in the product
// of two numbers
import java.io.*;
import java.math.*;
 
class GFG {
     
    // function to count number of digits
    // in the product of two numbers
    static int countDigits(int a, int b) {
        int count = 0;
         
        // absolute value of the
        // product of two numbers
        int p = Math.abs(a * b);
         
        // if product is 0
        if (p == 0) {
            return 1;
        }
        // count number of digits in
        // the product 'p'
        while (p > 0) {
            count++;
            p = p / 10;
        }
         
        // required count of digits
        return count;
    }
     
    // Driver program to test above
    public static void main(String args[])
    {
        int a = 33;
        int b = -24;
          // Function call
        System.out.println("Number of digits = "
                           + countDigits(a, b));
    }
}
 
/*This code is contributed by Nikita Tiwari.*/


Python3




# Python 3 implementation to count
# number of digits in the product
# of two numbers
 
# function to count number of digits
# in the product of two numbers
def countDigits(a, b) :
    count = 0
     
    # absolute value of the
    # product of two numbers
    p = abs(a * b)
     
    # if product is 0
    if (p == 0) :
        return 1
     
    # count number of digits
    # in the product 'p'
    while (p > 0) :
        count = count + 1
        p = p // 10
     
     
    # required count of digits
    return count
 
 
# Driver program to test above
a = 33
b = -24
# Function call
print("Number of digits = ",
       countDigits(a,b))
 
# This code is contributed by Nikita Tiwari.


C#




// C# implementation to count
// number of digits in the product
// of two numbers
using System;
 
class GFG {
     
    // function to count number of digits
    // in the product of two numbers
    static int countDigits(int a, int b) {
        int count = 0;
 
        // absolute value of the
        // product of two numbers
        int p = Math.Abs(a * b);
 
        // if product is 0
        if (p == 0) {
            return 1;
        }
 
        // count number of digits in
        // the product 'p'
        while (p > 0) {
            count++;
            p = p / 10;
        }
 
        // required count of digits
        return count;
    }
 
    // Driver program to test above
    public static void Main()
    {
        int a = 33;
        int b = -24;
          // Function call
        Console.WriteLine("Number of digits = " +
                              countDigits(a, b));
    }
}
 
// This code is contributed by Sam007


PHP




<?php
// PHP implementation to count
// number of digits in the
// product of two numbers
 
// function to count number
// of digits in the product
// of two numbers
function countDigits($a, $b)
{
    $count = 0;
     
    // absolute value of the
    // product of two numbers
    $p = abs($a * $b);
     
    // if product is 0
    if ($p == 0) {
        return 1;
    }
    // count number of digits
    // in the product 'p'
    while ($p > 0)
    {
        $count++;
        $p = (int)($p / 10);
    }
     
    // required count of digits
    return $count;
}
 
// Driver Code
$a = 33;
$b = -24;
echo "Number of digits = " .
        countDigits($a, $b);
 
// This code is contributed by mits
?>


Javascript




<script>
    // Javascript implementation to count number of digits
    // in the product of two numbers
     
    // function to count number of digits
    // in the product of two numbers
    function countDigits(a, b)
    {
        let count = 0;   
 
        // absolute value of the
        // product of two numbers
        let p = Math.abs(a*b);
 
        // if product is 0
        if (p == 0) {  
            return 1;
        }
        // count number of digits in the product 'p'   
        while (p > 0)   
        {
            count++;
            p = parseInt(p / 10, 10);
        }
 
        // required count of digits   
        return count;
    }
     
    let a = 33;
    let b = -24;
    document.write("Number of digits = " + countDigits(a,b));
 
// This code is contributed by divyeshrabadiya07.
</script>


Output: 
 

Number of digits = 3

Time Complexity: O(log(abs(a*b)))  where abs is absolute value of a*b.
Auxiliary Space: O(1)

Another  Approach: To count the number of digits in the product of two numbers we can use the formula given below:  Here time complexity will remain same.
 

                count = floor(log10(a) + log10(b)) + 1

Here both the numbers need to be positive integers. For this we can take the absolute values of a and b
 

C++




// C++ implementation to count number of digits
// in the product of two numbers
#include <bits/stdc++.h>
 
using namespace std;
 
// function to count number of digits
// in the product of two numbers
int countDigits(int a, int b)
{
    // if either of the number is 0, then
    // product will be 0
    if (a == 0 || b == 0){
        return 1;
    }
    // required count of digits           
    return floor(log10(abs(a)) + log10(abs(b))) + 1;   
}
 
// Driver program to test above
int main()
{
    int a = 33;
    int b = -24;
      // Function call
    cout << countDigits(a,b);
    return 0;
}


Java




// JAVA Code for Number of digits
// in the product of two numbers
class GFG {
     
    // function to count number of digits
    // in the product of two numbers
    public static int countDigits(int a, int b)
    {
        // if either of the number is 0, then
        // product will be 0
        if (a == 0 || b == 0) {
            return 1;
        }
        // required count of digits           
        return (int)Math.floor(Math.log10(Math.abs(a)) +
                            Math.log10(Math.abs(b))) + 1;   
    }
     
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int a = 33;
        int b = -24;
          // Function call
        System.out.print(countDigits(a,b));
    }
}
// This code is contributed by Arnav Kr. Mandal.


Python3




# Python 3 implementation to count
# number of digits in the product
# of two numbers
import math
 
# function to count number of digits
# in the product of two numbers
def countDigits(a, b) :
     
    # if either of the number is 0,
    # then product will be 0
    if (a == 0 or b == 0) :
        return 1
         
    # required count of digits        
    return math.floor(math.log10(abs(a)) +
                   math.log10(abs(b))) + 1
 
 
# Driver program to test above
a = 33
b = -24
# Function call
print(countDigits(a, b))
 
# This code is contributed by Nikita Tiwari.


C#




// C# Code for Number of digits
// in the product of two numbers
using System;
 
class GFG {
     
    // function to count number of
    // digits in the product of two
    // numbers
    public static int countDigits(int a,
                                  int b)
    {
        // if either of the number is 0,
        // then product will be 0
        if (a == 0 || b == 0) {
            return 1;
        }
        // required count of digits        
        return (int)Math.Floor(
                 Math.Log10(Math.Abs(a))
          + Math.Log10(Math.Abs(b))) + 1;
    }
     
    // Driver code
    static void Main()
    {
        int a = 33;
        int b = -24;
        Console.Write(countDigits(a, b));
         
    }
}
 
// This code is contributed by Sam007


PHP




<?php
// PHP implementation to count
// number of digits in the product
// of two numbers
 
// function to count number of digits
// in the product of two numbers
function countDigits($a, $b)
{
    // if either of the number is
    // 0, then product will be 0
    if ($a == 0 or $b == 0) {
        return 1;
    }
    // required count of digits    
    return floor(log10(abs($a)) +
                 log10(abs($b))) + 1;
}
 
// Driver Code
$a = 33;
$b = -24;
echo countDigits($a, $b);
 
// This code is contributed by mits
?>


Javascript




<script>
 
// Javascript implementation to count number of digits
// in the product of two numbers
 
// function to count number of digits
// in the product of two numbers
function countDigits(a, b)
{
    // if either of the number is 0, then
    // product will be 0
    if (a == 0 || b == 0) {
        return 1;
    }
    // required count of digits           
    return Math.floor(Math.log10(Math.abs(a)) + Math.log10(Math.abs(b))) + 1;   
}
 
// Driver program to test above
    let a = 33;
    let b = -24;
    document.write(countDigits(a,b));
 
// This code is contributed by Mayank Tyagi
 
</script>


Output: 
 

3

Time Complexity: O(loga + logb) = O(log(a*b))
Auxiliary Space: O(1)
 

This article is contributed by Ayush Jauhari. If you like neveropen and would like to contribute, you can also write an article using write.neveropen.co.za or mail your article to review-team@neveropen.co.za. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments