Write a program to check whether all digits of the given number are same type or not

JavaServer Side ProgrammingProgramming

To check if all digits of a number divide it, the Java code is as follows −

Example

 Live Demo

import java.io.*; public class Demo{    static boolean divisibility_check(int val, int digit){       return (digit != 0 && val % digit == 0);    }    static boolean divide_digits(int val){       int temp = val;       while (temp > 0){          int digit = val % 10;          if ((divisibility_check(val, digit)) == false)          return false;          temp /= 10;       }       return true;    }    public static void main(String args[]){       int val = 150;       if (divide_digits(val))       System.out.println("All the digits of the number divide the number completely.");       else       System.out.println("All the digits of the number are not divided by the number       completely.");    } }

Output

All the digits of the number are not divided by the number completely.

A class named Demo contains a function named ‘divisibility_check’, which has two parameters- the number and the digit. This function returns a Boolean value depending on whether the output returned is true or false. It checks if the number is not 0 and if the number divided by digit of the number is completely divided or not.

Another function named ‘divide_digits’ is a Boolean function that takes the number as the parameter. This function checks to see if all the digits in a number divide the number fully. In the main function, a value for the number is defined and the function is called with this value, if it returns ‘true’, then relevant message is displayed. If not, then it gives a message stating the number can’t be completely divided.

Write a program to check whether all digits of the given number are same type or not

Updated on 08-Jul-2020 11:34:00

/* program to check whether the last digit of three numbers is same */ // take input const a = prompt('Enter a first integer: '); const b = prompt('Enter a second integer: '); const c = prompt('Enter a third integer: '); // find the last digit const result1 = a % 10; const result2 = b % 10; const result3 = c % 10; // compare the last digits if(result1 == result2 && result1 == result3) { console.log(`${a}, ${b} and ${c} have the same last digit.`); } else { console.log(`${a}, ${b} and ${c} have different last digit.`); }

Output

Enter a first integer: 8 Enter a second integer: 38 Enter a third integer: 88 8, 38 and 88 have the same last digit.

In the above example, the user is asked to enter three integers.

The three integer values are stored in variables a, b and c.

The last digit of an integer value is calculated using a modulus operator %.

% gives the remainder value. For example, 58 % 10 gives 8.

All the last digits are then compared using if..else statement and logical AND operator && operator.

I want to show an alert if all the 5 digits in a given 5 digit number are same for example 11111, 22222, etc. Here is my fiddle http://jsfiddle.net/09k8f5s4/36/ and below is my code which is not working. Any help would be appreciated.

<div ng-app="app" ng-controller="test"> <input type="text" ng-model="value"/> <button ng-click=check()>Check</button> </div> angular.module('app', []) .controller('test', function($scope) { $scope.check = function() { if(/(\d)\1{2}-\1{3}-\1{4}/.test($scope.value)) { alert("Invalid number"); } } });

Last update on August 19 2022 21:51:54 (UTC/GMT +8 hours)

Write a JavaScript program to check whether all the digits in a given number are the same or not.

Pictorial Presentation:

Write a program to check whether all digits of the given number are same type or not

Sample Solution:

HTML Code:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Check whether all the digits in a given number are the same or not</title> </head> <body> </body> </html>

JavaScript Code:

function test_same_digit(num) { var first = num % 10; while (num) { if (num % 10 !== first) return false; num = Math.floor(num / 10); } return true } console.log(test_same_digit(1234)); console.log(test_same_digit(1111)); console.log(test_same_digit(22222222));

Sample Output:

false true true

Flowchart:

Write a program to check whether all digits of the given number are same type or not

ES6 Version:

function test_same_digit(num) { const first = num % 10; while (num) { if (num % 10 !== first) return false; num = Math.floor(num / 10); } return true } console.log(test_same_digit(1234)); console.log(test_same_digit(1111)); console.log(test_same_digit(22222222));

Live Demo:

See the Pen javascript-basic-exercise-140 by w3resource (@w3resource) on CodePen.

Improve this sample solution and post your code through Disqus

Previous:Write a JavaScript program to find the position of a rightmost round number in an array of integers. Returns 0 if there are no round number.
Next: Write a JavaScript program to find the number of elements which presents in both of the given arrays.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Spread operator

const user = { name: 'Owen', age: 21 }; const admin = { admin: true, ...user }; console.log(admin);

It's possible to combine objects using the spread operator .... It lets you create copies of the key/value pairs of one object, and add them to another object. In this case, we create copies of the user object, and add them to the admin object. The admin object now contains the copied key/value pairs, which results in { admin: true, name: "Owen", age: 21 }.

Ref: https://bit.ly/323Y0P6

  • Exercises: Weekly Top 16 Most Popular Topics
  • SQL Exercises, Practice, Solution - JOINS
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • JavaScript basic - Exercises, Practice, Solution
  • Java Array: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : Conditional Statement
  • HR Database - SORT FILTER: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : String
  • Python Data Types: Dictionary - Exercises, Practice, Solution
  • Python Programming Puzzles - Exercises, Practice, Solution
  • C++ Array: Exercises, Practice, Solution
  • JavaScript conditional statements and loops - Exercises, Practice, Solution
  • C# Sharp Basic Algorithm: Exercises, Practice, Solution
  • Python Lambda - Exercises, Practice, Solution
  • Python Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation

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

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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




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

Write a program to check whether all digits of the given number are same type or not

=> Sum =

Write a program to check whether all digits of the given number are same type or not

=> Sum =

Write a program to check whether all digits of the given number are same type or not

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




Article Tags :

Mathematical

School Programming

Geometric Progression

number-digits