Check if all the digits of the given number are same - GeeksforGeeks

08 Sep, 2021
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 = 222Output: Yes
Input: N = 232Output: 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++
Java
Python3
C#
Javascript
// C++ Program to implement// the above approach#include <bits/stdc++.h>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 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));    }}
# 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# 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.
<script> // javascript Program to implement// the above approach // Function to check if all the digits// in the number N is the same or notfunction checkSameDigits(N){       // Find the last digit    var digit = N % 10;     while (N != 0)    {         // Find the current last digit        var current_digit = N % 10;         // Update the value of N        N = parseInt(N / 10);         // If there exists any distinct        // digit, then return No        if (current_digit != digit)        {            return "No";        }    }     // Otherwise, return Yes    return "Yes";} // Driver Code    var N = 222;    document.write(checkSameDigits(N));         // This code is contributed by ipg2016107.</script>
  
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++
Java
Python3
C#
Javascript
#include <iostream>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 = (N) + 1;     // Form the number M of the type    // K*111... where K is the    // rightmost digit of N    int M = ((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 shivanisinghss2110
// 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 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# 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.
<script> // JavaScript program for the above approach// Function to check if all the digits// in the number N is the same or notfunction checkSameDigits(N)    {             // Get the length of N        var length = (Math.log10(N)) + 1;         // Form the number M of the type        // K*111... where K is the        // rightmost digit of N        var M = (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 Codevar N = 222;document.write(checkSameDigits(N));     // This code is contributed by shivanisinghss2110</script>
  
Yes
 
 
Time Complexity: O(1)Auxiliary Space: O(1)
 
lokeshpotta20
ukasp
ankthon
ipg2016107
subhammahato348
shivanisinghss2110
Geometric Progression
number-digits
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Modular multiplicative inverse
Count all possible paths from top left to bottom right of a mXn matrix
Fizz Buzz Implementation
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects