Check if two numbers have same number of digits

Given two integers A and B, the task is to check whether both the numbers have equal number of digits.

Examples:

Input: A = 12, B = 1
Output: No

Input: A = 20, B = 99
Output: Yes

Approach: While both the numbers are > 0, keep dividing both the numbers by 10. Finally, check if both the numbers are 0. If any one of them is not 0 then they had unequal number of digits.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that return true if A and B
// have same number of digits
bool sameLength(int A, int B)
{
    while (A > 0 && B > 0) {
        A = A / 10;
        B = B / 10;
    }
  
    // Both must be 0 now if
    // they had same lengths
    if (A == 0 && B == 0)
        return true;
    return false;
}
  
// Driver code
int main()
{
    int A = 21, B = 1;
  
    if (sameLength(A, B))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.io.*;
  
class GFG 
{
      
// Function that return true if A and B
// have same number of digits
static boolean sameLength(int A, int B)
{
    while ((A > 0) && (B > 0))
    {
        A = A / 10;
        B = B / 10;
    }
  
    // Both must be 0 now if
    // they had same lengths
    if ((A == 0 )&& (B == 0))
        return true;
    return false;
}
  
// Driver code
public static void main (String[] args) 
{
  
    int A = 21, B = 1;
    if (sameLength(A, B))
        System.out.println ("Yes");
    else
        System.out.println("No");
  
}
}
  
// This code is contributed by @tushil. 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

      
# Python implementation of the approach
  
# Function that return true if A and B
# have same number of digits
def sameLength(A, B):
    while (A > 0 and B > 0):
        A = A / 10;
        B = B / 10;
  
    # Both must be 0 now if
    # they had same lengths
    if (A == 0 and B == 0):
        return True;
    return False;
  
# Driver code
A = 21; B = 1;
  
if (sameLength(A, B)):
    print("Yes");
else:
    print("No");
  
# This code contributed by PrinciRaj1992

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
          
// Function that return true if A and B
// have same number of digits
static bool sameLength(int A, int B)
{
    while ((A > 0) && (B > 0))
    {
        A = A / 10;
        B = B / 10;
    }
  
    // Both must be 0 now if
    // they had same lengths
    if ((A == 0 )&& (B == 0))
        return true;
    return false;
}
  
// Driver code
static public void Main ()
{
          
    int A = 21, B = 1;
    if (sameLength(A, B))
            Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
  
}
}
  
// This code is contributed by ajit.. 

chevron_right


Output:

No


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t, princiraj1992



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.