# Check if all the digits of the given number are same

Given a positive integer N, the task is to check whether all the digits of the given integer N are the same or not. If found to be true, then print Yes. Otherwise, print No.

Examples:

Input: N = 222
Output: Yes

Input: N = 232
Output: No

Naive Approach: The simplest approach to solve the given problem is to iterate over all the digits of the given number N and if there exists any distinct digit then print Yes. Otherwise, print No.

Below is the implementation of the above approach:

## C++

 // C++ Program to implement// the above approach#include using namespace std; // Function to check if all the digits// in the number N is the same or notstring checkSameDigits(int N){       // Find the last digit    int digit = N % 10;     while (N != 0)    {         // Find the current last digit        int current_digit = N % 10;         // Update the value of N        N = N / 10;         // If there exists any distinct        // digit, then return No        if (current_digit != digit)        {            return "No";        }    }     // Otherwise, return Yes    return "Yes";} // Driver Codeint main(){    int N = 222;    cout << (checkSameDigits(N));    return 0;} // This code is contributed by Potta Lokesh

## Java

 // Java program for the above approach import java.io.*; class GFG {     // Function to check if all the digits    // in the number N is the same or not    public static String checkSameDigits(int N)    {        // Find the last digit        int digit = N % 10;         while (N != 0) {             // Find the current last digit            int current_digit = N % 10;             // Update the value of N            N = N / 10;             // If there exists any distinct            // digit, then return No            if (current_digit != digit) {                return "No";            }        }         // Otherwise, return Yes        return "Yes";    }     // Driver Code    public static void main(String args[])        throws IOException    {        int N = 222;        System.out.println(            checkSameDigits(N));    }}

## Python3

 # Python Program to implement# the above approach # Function to check if all the digits# in the number N is the same or notdef checkSameDigits(N) :       # Find the last digit    digit = N % 10;     while (N != 0) :         # Find the current last digit        current_digit = N % 10;         # Update the value of N        N = N // 10;         # If there exists any distinct        # digit, then return No        if (current_digit != digit) :            return "No";     # Otherwise, return Yes    return "Yes"; # Driver Codeif __name__ == "__main__" :     N = 222;    print(checkSameDigits(N));        # This code is contributed by AnkThon

## C#

 // C# Program to implement// the above approachusing System;class GFG {     // Function to check if all the digits    // in the number N is the same or not    static string checkSameDigits(int N)    {         // Find the last digit        int digit = N % 10;         while (N != 0) {             // Find the current last digit            int current_digit = N % 10;             // Update the value of N            N = N / 10;             // If there exists any distinct            // digit, then return No            if (current_digit != digit) {                return "No";            }        }         // Otherwise, return Yes        return "Yes";    }     // Driver Code    public static void Main()    {        int N = 222;        Console.Write(checkSameDigits(N));    }} // This code is contributed by divyesh972019.

## Javascript



Output
Yes

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

Efficient Approach: The above approach can also be optimized by forming another number, say M of the same length of the given number N with the rightmost digit of N assuming N has all same digits and then comparing it with N. Now, M is of type (K*111….), where K is any digit from N.

Now to create the number M consisting of the only 1s, the sum of a Geometric Progression can be used as illustrated for the count of digits as 3:

Consider the first term(say a) as 1 and the common ratio(say r) as 10. Now for the value count of digits(say D) as 3 the sum of Geometric Progression is given by:

=> Sum =

=> Sum =

=> Sum =

-> Sum = 111

From the above observations, generate the number M and check if K*M is the same as the N or not. If found to be true, then print Yes. Otherwise, print No.

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach #include using namespace std; // Function to check if all the digits// in the number N is the same or notstring checkSameDigits(int N){       // Get the length of N    int length = int(log10(N)) + 1;     // Form the number M of the type    // K*111... where K is the    // rightmost digit of N    int M = (int(pow(10, length)) - 1) / (10 - 1);    M *= N % 10;     // Check if the numbers are equal    if (M == N)        return "Yes";     // Otherwise    return "No";} // Driver Codeint main() {    int N = 222;    cout << checkSameDigits(N);}// This code is contributed by Pushpesh raj

## Java

 // Java program for the above approach import java.io.*; class GFG {     // Function to check if all the digits    // in the number N is the same or not    public static String checkSameDigits(int N)    {        // Get the length of N        int length = ((int)Math.log10(N)) + 1;         // Form the number M of the type        // K*111... where K is the        // rightmost digit of N        int M = ((int)Math.pow(10, length) - 1)                / (10 - 1);        M *= N % 10;         // Check if the numbers are equal        if (M == N)            return "Yes";         // Otherwise        return "No";    }     // Driver Code    public static void main(String args[])        throws IOException    {        int N = 222;        System.out.println(            checkSameDigits(N));    }}

## Python3

 # Python3 program for the above approachimport math # Function to check if all the digits# in the number N is the same or notdef checkSameDigits(N) :         # Get the length of N    length = int(math.log10(N)) + 1;     # Form the number M of the type    # K*111... where K is the    # rightmost digit of N    M = (int(math.pow(10, length)) - 1)// (10 - 1);    M *= N % 10;     # Check if the numbers are equal    if (M == N) :        return "Yes";     # Otherwise    return "No";     # Driver Codeif __name__ == "__main__" :         N = 222;    print(checkSameDigits(N));         # This code is contributed by AnkThon

## C#

 // C# program for the above approach using System; class GFG {     // Function to check if all the digits    // in the number N is the same or not    public static String checkSameDigits(int N)    {        // Get the length of N        int length = ((int)Math.Log10(N)) + 1;         // Form the number M of the type        // K*111... where K is the        // rightmost digit of N        int M = ((int)Math.Pow(10, length) - 1) / (10 - 1);        M *= N % 10;         // Check if the numbers are equal        if (M == N)            return "Yes";         // Otherwise        return "No";    }     // Driver Code    public static void Main()    {        int N = 222;        Console.WriteLine(checkSameDigits(N));    }} // This code is contributed by subhammahato348.

## Javascript



Output
Yes

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

Approach:

• Convert the given integer N to a string representation using the to_string function. This allows us to access individual digits as characters.
• Declare a variable digit and initialize it with the first character of the string str, which represents the first digit of the number.
• Iterate through the remaining characters of the string str, starting from the second character (index 1). Compare each character with the reference digit stored in digit.
• If any character is found to be different from the reference digit, return “No” indicating that not all digits are the same.
• If the loop completes without finding any different digit, return “Yes” indicating that all digits are the same.

Below is the implementation of the above approach:

## C++

 #include #include using namespace std; // Function to check if all the digits// in the number N is the same or notstring checkSameDigits(int N){    string str = to_string(N);    char digit = str[0];     for (int i = 1; i < str.length(); i++)    {        if (str[i] != digit)        {            return "No";        }    }     return "Yes";} // Driver codeint main(){    int N = 222;    cout << checkSameDigits(N);    return 0;}

## Java

 import java.util.*; public class GFG {     // Function to check if all the digits    // in the number N is the same or not    public static String checkSameDigits(int N)    {        String str = Integer.toString(N);        char digit = str.charAt(0);         for (int i = 1; i < str.length(); i++) {            if (str.charAt(i) != digit) {                return "No";            }        }         return "Yes";    }     // Driver code    public static void main(String[] args)    {        int N = 222;        System.out.println(checkSameDigits(N));    }}// This code is contributed by shivamgupta0987654321

## Python3

 def check_same_digits(N):    str_N = str(N)  # Convert the number to a string     digit = str_N[0]  # Get the first digit of the number     # Iterate through the remaining digits of the number    for i in range(1, len(str_N)):        if str_N[i] != digit:  # If any digit is different from the first digit            return "No"  # Return "No" indicating that the digits are not all the same     return "Yes"  # If all digits are the same, return "Yes" # Driver codeN = 222print(check_same_digits(N))

## C#

 using System; class GFG {    // Function to check if all digits of a number are the    // same    static string CheckSameDigits(int N)    {        string str_N = N.ToString(); // Convert the number                                     // to a string         char digit            = str_N[0]; // Get the first digit of the number         // Iterate through the remaining digits of the        // number        for (int i = 1; i < str_N.Length; i++) {            if (str_N[i]                != digit) // If any digit is different from                          // the first digit            {                return "No"; // Return "No" indicating that                             // the digits are not all the                             // same            }        }         return "Yes"; // If all digits are the same, return                      // "Yes"    }     static void Main()    {        int N = 222;        Console.WriteLine(CheckSameDigits(N));    }} // This code is contributed by shivamgupta0987654321

## Javascript

 // Function to check if all the digits// in the number N are the same or notfunction checkSameDigits(N) {    let str = N.toString();    let digit = str[0];     for (let i = 1; i < str.length; i++) {        if (str[i] !== digit) {            return "No";        }    }     return "Yes";} // Driver codeconst N = 222;console.log(checkSameDigits(N));

Output
Yes

Time Complexity: O(d), where d is the number of digits in the given number N.
Auxiliary Space: O(d), where d is the number of digits in the given number N, The subsequent loop iterates through the characters of the string str, which also takes O(d) time.

Previous
Next