Number of digits in the product of two numbers
Last Updated :
14 Feb, 2023
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)
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++
#include <bits/stdc++.h>
using namespace std;
int countDigits( int a, int b){
int count = 0;
int p = abs (a*b);
if (p == 0) {
return 1;
}
while (p > 0){
count++;
p = p / 10;
}
return count;
}
int main()
{
int a = 33;
int b = -24;
cout << "Number of digits = "
<< countDigits(a,b);
return 0;
}
|
Java
import java.io.*;
import java.math.*;
class GFG {
static int countDigits( int a, int b) {
int count = 0 ;
int p = Math.abs(a * b);
if (p == 0 ) {
return 1 ;
}
while (p > 0 ) {
count++;
p = p / 10 ;
}
return count;
}
public static void main(String args[])
{
int a = 33 ;
int b = - 24 ;
System.out.println( "Number of digits = "
+ countDigits(a, b));
}
}
|
Python3
def countDigits(a, b) :
count = 0
p = abs (a * b)
if (p = = 0 ) :
return 1
while (p > 0 ) :
count = count + 1
p = p / / 10
return count
a = 33
b = - 24
print ( "Number of digits = " ,
countDigits(a,b))
|
C#
using System;
class GFG {
static int countDigits( int a, int b) {
int count = 0;
int p = Math.Abs(a * b);
if (p == 0) {
return 1;
}
while (p > 0) {
count++;
p = p / 10;
}
return count;
}
public static void Main()
{
int a = 33;
int b = -24;
Console.WriteLine( "Number of digits = " +
countDigits(a, b));
}
}
|
PHP
<?php
function countDigits( $a , $b )
{
$count = 0;
$p = abs ( $a * $b );
if ( $p == 0) {
return 1;
}
while ( $p > 0)
{
$count ++;
$p = (int)( $p / 10);
}
return $count ;
}
$a = 33;
$b = -24;
echo "Number of digits = " .
countDigits( $a , $b );
?>
|
Javascript
<script>
function countDigits(a, b)
{
let count = 0;
let p = Math.abs(a*b);
if (p == 0) {
return 1;
}
while (p > 0)
{
count++;
p = parseInt(p / 10, 10);
}
return count;
}
let a = 33;
let b = -24;
document.write( "Number of digits = " + countDigits(a,b));
</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++
#include <bits/stdc++.h>
using namespace std;
int countDigits( int a, int b)
{
if (a == 0 || b == 0){
return 1;
}
return floor ( log10 ( abs (a)) + log10 ( abs (b))) + 1;
}
int main()
{
int a = 33;
int b = -24;
cout << countDigits(a,b);
return 0;
}
|
Java
class GFG {
public static int countDigits( int a, int b)
{
if (a == 0 || b == 0 ) {
return 1 ;
}
return ( int )Math.floor(Math.log10(Math.abs(a)) +
Math.log10(Math.abs(b))) + 1 ;
}
public static void main(String[] args)
{
int a = 33 ;
int b = - 24 ;
System.out.print(countDigits(a,b));
}
}
|
Python3
import math
def countDigits(a, b) :
if (a = = 0 or b = = 0 ) :
return 1
return math.floor(math.log10( abs (a)) +
math.log10( abs (b))) + 1
a = 33
b = - 24
print (countDigits(a, b))
|
C#
using System;
class GFG {
public static int countDigits( int a,
int b)
{
if (a == 0 || b == 0) {
return 1;
}
return ( int )Math.Floor(
Math.Log10(Math.Abs(a))
+ Math.Log10(Math.Abs(b))) + 1;
}
static void Main()
{
int a = 33;
int b = -24;
Console.Write(countDigits(a, b));
}
}
|
PHP
<?php
function countDigits( $a , $b )
{
if ( $a == 0 or $b == 0) {
return 1;
}
return floor (log10( abs ( $a )) +
log10( abs ( $b ))) + 1;
}
$a = 33;
$b = -24;
echo countDigits( $a , $b );
?>
|
Javascript
<script>
function countDigits(a, b)
{
if (a == 0 || b == 0) {
return 1;
}
return Math.floor(Math.log10(Math.abs(a)) + Math.log10(Math.abs(b))) + 1;
}
let a = 33;
let b = -24;
document.write(countDigits(a,b));
</script>
|
Output:
3
Time Complexity: O(loga + logb) = O(log(a*b))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...