Java Program to Check if all digits of a number divide it Last Updated : 12 Jun, 2022 Comments Improve Suggest changes Like Article Like Report 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 % d == 0 for d = 1, 2, 8. To do that, we need to iterate over each digit of the number. Java // Java program to check whether // number is divisible by all its digits. import java.io.*; class GFG { // Function to check the divisibility // of the number by its digit. static boolean checkDivisibility(int n, int digit) { // If the digit divides the number // then return true else return false. return (digit != 0 && n % digit == 0); } // Function to check if all // digits of n divide it or not, static boolean allDigitsDivide(int n) { int temp = n; while (temp > 0) { // Taking the digit of the // number into var 'digit'. int digit = n % 10; if ((checkDivisibility(n, digit)) == false) return false; temp /= 10; } return true; } // Driver function public static void main(String args[]) { int n = 128; // function call to check // digits divisibility if (allDigitsDivide(n)) System.out.println("Yes"); else System.out.println("No"); } } /*This code is contributed by Nikita Tiwari.*/ Output: Yes Time Complexity: O(log10n), where n represents the given integer.Auxiliary Space: O(1), no extra space is required, so it is a constant. Please refer complete article on Check if all digits of a number divide it for more details! Comment More infoAdvertise with us Next Article Java Program to Check if all digits of a number divide it kartik Follow Improve Article Tags : Java Practice Tags : Java Similar Reads 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 = 222Output: Yes Input: N = 232Output: No Recommended: Please try your approach on {IDE} first, before movi 11 min read Java Program to check whether it is possible to make a divisible by 3 number using all digits in an array Given an array of integers, the task is to find whether it's possible to construct an integer using all the digits of these numbers such that it would be divisible by 3. If it is possible then print "Yes" and if not print "No". Examples: Input : arr[] = {40, 50, 90} Output : Yes We can construct a n 2 min read Java Program to Check if a Given Number is Perfect Number A number is said to be a perfect number if the sum of its proper divisors ( i.e. all positive divisors excluding the number itself )is equal to that number itself. Aliquot sum is the sum of divisors of a number, excluding the number itself. Hence, a number is a perfect number only if it is equal to 5 min read Java Program to Check Whether Number is Divisible by 5 Generic rules in mathematics to test divisibility by 5 in a number system is followed as numbers that end in 5 or 0 are divisible by 5. It doesn't matter how big the number is. Modulo operator will be used most frequently in programming when it comes down to divisibility. Knowledge of the range of n 4 min read How to Check if a String Contains only Digits in Java? In Java, to check if a string contains only digits, we can use various methods such as simple iteration, regular expressions, streams, etc.Example:The example below uses the Character.isDigit() method to check each character of the string to ensure all characters are digits. This is the most simple 3 min read Java Program to Count rotations which are divisible by 10 Given a number N, the task is to count all the rotations of the given number which are divisible by 10.Examples: Input: N = 10203 Output: 2 Explanation: There are 5 rotations possible for the given number. They are: 02031, 20310, 03102, 31020, 10203 Out of these rotations, only 20310 and 31020 are d 2 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 number is divisible by any of its digits Given an integer N where 1 \leq n \leq 10^{18} . The task is to check whether the number is not divisible by any of its digit. If the given number N is divisible by any of its digits then print "YES" else print "NO". Examples: Input : N = 5115Output : YESExplanation: 5115 is divisible by both 1 and 10 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 Check if a large number is divisible by 11 or not Given a number, the task is to check if the number is divisible by 11 or not. The input number may be large and it may not be possible to store it even if we use long long int.Examples: Input : n = 76945Output : YesInput : n = 1234567589333892Output : YesInput : n = 363588395960667043875487Output : 7 min read Like