Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to check if all digits of a number divide it
The given article involves determining if all digits of a positive integer can divide the number without leaving a remainder. If any digit is zero or does not divide the number the result is false; otherwise, it is true using Java. This can be solved using two approaches: the Naive-Based Approach, which relies on arithmetic operations, and the String-Based Approach, which uses string manipulation for digit processing. Both methods ensure each digit meets the divisibility condition efficiently.
Approaches to Check If All Digits of a Number Divide It
Following are the different approaches to check if all digits of a number divide it ?
Using Naive Approach
Inside the divisibility_check method, the condition digit != 0 && val % digit == 0 is used. It checks if the digit is not zero to avoid division by zero and if the number is divisible by the digit without a remainder. This ensures that only valid digits that divide the number are considered.
static boolean divisibility_check(int val, int digit){
return (digit != 0 && val % digit == 0);
}
A while loop is used in the divide_digits method to extract each digit of the number (val % 10). The loop iterates until all digits are processed (temp > 0) and calls the divisibility_check method for each digit. The loop breaks early if any digit fails the divisibility condition.
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;
}
Example
To check if all digits of a number divide it, the Java code is as follows ?
import java.io.*;
public class Demo {
// Method to check if the number is divisible by the digit
static boolean divisibility_check(int val, int digit) {
return (digit != 0 && val % digit == 0); // Checks if the digit divides the number
}
// Method to check if all digits of the number divide the number completely
static boolean divide_digits(int val) {
int temp = val; // Copy of the original value
while (temp > 0) {
int digit = temp % 10; // Extract the last digit from temp
if ((divisibility_check(val, digit)) == false) // Check divisibility for each digit
return false; // Return false if any digit does not divide the number
temp /= 10; // Remove the last digit
}
return true; // Return true if all digits divide the number
}
public static void main(String args[]) {
int val = 150; // Number to check
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
Time Complexity: O(d), where d is the number of digits in the number. The algorithm processes each digit exactly once.
Space Complexity: O(1), as it uses a constant amount of additional memory.
Using String-Based Approach
Instead of extracting digits one by one in a loop, we can utilize the string-based approach to directly access each digit of the number. This method avoids mathematical operations like modulus (%) and division (/) and works by converting the number to a string. Each digit is then converted back to an integer for divisibility checks.
Example
public class DigitDividerCheck {
// Method to check if all digits of the number divide it
public static boolean doAllDigitsDivide(int number) {
int originalNumber = number; // Store the original number for division checks
while (number > 0) {
int digit = number % 10; // Extract the last digit
// Check for division by zero or if the digit does not divide the number
if (digit == 0 || originalNumber % digit != 0) {
return false; // Return false if any condition is not met
}
number /= 10; // Remove the last digit
}
return true; // Return true if all digits divide the number
}
public static void main(String[] args) {
// Test cases
int num1 = 128;
int num2 = 132;
int num3 = 101;
System.out.println("Does " + num1 + " satisfy the condition? " + doAllDigitsDivide(num1));
System.out.println("Does " + num2 + " satisfy the condition? " + doAllDigitsDivide(num2));
System.out.println("Does " + num3 + " satisfy the condition? " + doAllDigitsDivide(num3));
}
}
Output
Does 128 satisfy the condition? true Does 132 satisfy the condition? false Does 101 satisfy the condition? false
Time Complexity: O(log(n))
Space Complexity: O(1)
Difference Table: Naive-Based vs String-Based Approach
The table below highlights the key differences between the Naive-Based and String-Based approaches for determining if all digits of a number divide it completely ?
| Aspect |
Naive-Based Approach |
String-Based Approach |
| Methodology |
Uses arithmetic operations like modulus (%) and division (/) to extract and process digits. | Converts the number to a string for easy digit access. |
| Readability |
May involve more complex operations for beginners. |
Simpler and more intuitive for those familiar with strings. |
| Space Usage |
Minimal, as it does not require storing digits separately. |
Requires additional space to store the string representation of the number. |