Java Program to check if all digits of a number divide it


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.

Updated on: 08-Jul-2020

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements