Check if two numbers have same number of digits - GeeksforGeeks

10 May, 2021
Given two integers A and B, the task is to check whether both the numbers have an equal number of digits.Examples:  
Input: A = 12, B = 1 Output: NoInput: 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 an unequal numbers of digits.Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function that return true if A and B// have same number of digitsbool 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 codeint main(){    int A = 21, B = 1;     if (sameLength(A, B))        cout << "Yes";    else        cout << "No";     return 0;}
// Java implementation of the approachimport java.io.*; class GFG{     // Function that return true if A and B// have same number of digitsstatic 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 codepublic 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.
     # Python implementation of the approach # Function that return true if A and B# have same number of digitsdef 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 codeA = 21; B = 1; if (sameLength(A, B)):    print("Yes");else:    print("No"); # This code contributed by PrinciRaj1992
// C# implementation of the approachusing System; class GFG{         // Function that return true if A and B// have same number of digitsstatic 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 codestatic 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..
<script> // Javascript implementation of the approach // Function that return true if A and B// have same number of digitsfunction sameLength(A, B){    while (A > 0 && B > 0) {        A = parseInt(A / 10);        B = parseInt(B / 10);    }     // Both must be 0 now if    // they had same lengths    if (A == 0 && B == 0)        return true;    return false;} // Driver code    let A = 21, B = 1;     if (sameLength(A, B))        document.write("Yes");    else        document.write("No"); </script>
No
 
jit_t
princiraj1992
souravmahato348
number-digits
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Print all possible combinations of r elements in a given array of size n
Sieve of Eratosthenes
Operators in C / C++
The Knight's tour problem | Backtracking-1
Program for factorial of a number
Find minimum number of coins that make a given value