Check if all the digits of the given number are same
Last Updated :
18 Sep, 2023
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 <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>
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++
// C++ program for 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)
{
// 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 Code
int 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 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>
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 <iostream>
#include <string>
using namespace std;
// Function to check if all the digits
// in the number N is the same or not
string 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 code
int 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 code
N = 222
print(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 not
function 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 code
const N = 222;
console.log(checkSameDigits(N));
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.
Similar Reads
Check if the frequency of all the digits in a number is same
Given a positive number 'N', the task is to find whether 'N' is balanced or not. Output 'YES' if 'N' is a balanced number else 'NO'. A number is balanced if the frequency of all the digits in it is same i.e. all the digits appear the same number of times. Examples: Input: N = 1234567890 Output: YES
8 min read
Check if a number has digits in the given Order
Given a number N. The task is to check if the digits of the number follow any of the below order: The digits are in strictly increasing order.Or, the digits are in strictly decreasing order.Or, the digits follow strictly increasing order first and then strictly decreasing.If the number follows any o
9 min read
Check if the sum of digits of a number N divides it
Given a number n, the task is to check if the sum of digits of the given number divides the number or not. Examples: Input : n = 12Output : YesExplanation: Sum of digits = 1+2 =3 and 3 divides 12.Input : n = 15Output : NoExplanation: Sum of digits = 1+5 =6 and 15 % 6 != 0.Using Iterative Method - O(
6 min read
Count of numbers with all digits same in a given range
Given two integers L and R denoting the starting and end values of a range, the task is to count all numbers in that range whose all digit are same, like 1, 22, 444, 3333, etc.Example: Input: L = 12, R = 68 Output: 5 Explanation: { 22, 33, 44, 55, 66} are the numbers with same digits in the given ra
9 min read
Check if given number contains a digit which is the average of all other digits
Given an integer N, the task is to check whether N contains a digit D such that it is the average of all other digits present in N. Examples: Input: N = 132 Output: Yes Explanation: Since, (1 + 3)/2 = 2. Input: N = 436 Output: No Explanation: No such digit exists which is the average of all other di
5 min read
Check if a given number divides the sum of the factorials of its digits
Given an integer N, the task is to check whether N divides the sum of the factorials of its digits.Examples: Input: N = 19 Output: Yes 1! + 9! = 1 + 362880 = 362881, which is divisible by 19.Input: N = 20 Output: No 0! + 2! = 1 + 4 = 5, which is not divisible by 20. Approach: First, store the factor
9 min read
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 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 ar
3 min read
Check if all digits of a number divide it
Given a number n, find whether all digits of n divide it or not.Examples: Input : 128 Output : Yes 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. Input : 130 Output : No We want to test whether each digit is non-zero and divides the number. For example, with 128, we want to test d != 0 && 128
9 min read
Program to check if a given number is Lucky (all digits are different)
A number is lucky if all digits of the number are different. How to check if a given number is lucky or not.Examples: Input: n = 983 Output: true All digits are different Input: n = 9838 Output: false 8 appears twice We strongly recommend you to minimize your browser and try this yourself first.The
13 min read
Java Program to Check if all digits of a number divide it
Given a number n, find whether all digits of n divide it or not.Examples: Input : 128 Output : Yes 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. Input : 130 Output : No We want to test whether each digit is non-zero and divides the number. For example, with 128, we want to test d != 0 && 128
2 min read