Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Check if all the digits of the given number are same

  • Last Updated : 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:

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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 <bits/stdc++.h>
using namespace std;
 
// Function to check if all the digits
// in the number N is the same or not
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
int 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 not
def 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 Code
if __name__ == "__main__" :
 
    N = 222;
    print(checkSameDigits(N));
   
 
    # This code is contributed by AnkThon

C#




// C# Program to implement
// the above approach
using 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




<script>
 
// javascript Program to implement
// the above approach
 
// Function to check if all the digits
// in the number N is the same or not
function 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>

 
 

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 = \frac{a*(r^D - 1)}{r - 1}

=> Sum =  \frac{1*(10^3 - 1)}{10 - 1}

=> Sum = \frac{999}{9}

 -> 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++




#include <iostream>
using namespace std;
 
// Function to check if all the digits
// in the number N is the same or not
string 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 Code
int main()
 
{
    int N = 222;
    cout << checkSameDigits(N);
}
 
// This code is contributed by shivanisinghss2110

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 approach
import math
 
# Function to check if all the digits
# in the number N is the same or not
def 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 Code
if __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




<script>
 
// JavaScript program for the above approach
// Function to check if all the digits
// in the number N is the same or not
function 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 Code
var N = 222;
document.write(checkSameDigits(N));
     
// This code is contributed by shivanisinghss2110
</script>

 
 

Output: 
Yes

 

 

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

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :