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 % d == 0 for d = 1, 2, 8. To do that, we need to iterate over each digit of the number.
CPP
#include <bits/stdc++.h>
using namespace std;
bool checkDivisibility( int n, int digit)
{
return (digit != 0 && n % digit == 0);
}
bool allDigitsDivide( int n)
{
int temp = n;
while (temp > 0) {
int digit = temp % 10;
if (!(checkDivisibility(n, digit)))
return false ;
temp /= 10;
}
return true ;
}
int main()
{
int n = 128;
if (allDigitsDivide(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean checkDivisibility( int n, int digit)
{
return (digit != 0 && n % digit == 0 );
}
static boolean allDigitsDivide( int n)
{
int temp = n;
while (temp > 0 ) {
int digit = temp % 10 ;
if ((checkDivisibility(n, digit)) == false )
return false ;
temp /= 10 ;
}
return true ;
}
public static void main(String args[])
{
int n = 128 ;
if (allDigitsDivide(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def checkDivisibility(n, digit) :
return (digit ! = 0 and n % digit = = 0 )
def allDigitsDivide( n) :
temp = n
while (temp > 0 ) :
digit = temp % 10
if ((checkDivisibility(n, digit)) = = False ) :
return False
temp = temp / / 10
return True
n = 128
if (allDigitsDivide(n)) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool checkDivisibility( int n, int digit)
{
return (digit != 0 && n % digit == 0);
}
static bool allDigitsDivide( int n)
{
int temp = n;
while (temp > 0) {
int digit = temp % 10;
if ((checkDivisibility(n, digit)) == false )
return false ;
temp /= 10;
}
return true ;
}
public static void Main()
{
int n = 128;
if (allDigitsDivide(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function checkDivisibility( $n , $digit )
{
return ( $digit != 0 && $n % $digit == 0);
}
function allDigitsDivide( $n )
{
$temp = $n ;
while ( $temp > 0) {
$digit = $temp % 10;
if (!(checkDivisibility( $n , $digit )))
return false;
$temp /= 10;
}
return true;
}
$n = 128;
if (allDigitsDivide( $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function checkDivisibility(n, digit)
{
return (digit != 0 && n % digit == 0);
}
function allDigitsDivide(n)
{
let temp = n;
while (temp > 0)
{
let digit = temp % 10;
if (!(checkDivisibility(n, digit)))
return false ;
temp = parseInt(temp / 10, 10);
}
return true ;
}
let n = 128;
if (allDigitsDivide(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
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.
Alternate Implementation in Python
C++
#include <bits/stdc++.h>
using namespace std;
bool checkDivisibility( int n, int digit)
{
return (digit != 0 and n % digit == 0);
}
bool allDigitsDivide( int n)
{
set< int > nlist;
for ( char c : to_string(n))
nlist.insert(c - '0' );
for ( int digit : nlist) {
if (!checkDivisibility(n, digit))
return false ;
}
return true ;
}
int main()
{
int n = 128;
cout << (allDigitsDivide(n) ? "Yes" : "No" );
}
|
Java
import java.util.*;
class GFG {
static boolean checkDivisibility( int n, int digit)
{
return (digit != 0 && n % digit == 0 );
}
static boolean allDigitsDivide( int n)
{
HashSet<Character> nlist = new HashSet<Character>();
String nstr = String.valueOf(n);
for ( int i = 0 ; i < nstr.length(); i++) {
nlist.add(nstr.charAt(i));
}
for ( char digit : nlist) {
int digitVal = digit - '0' ;
if (!checkDivisibility(n, digitVal))
return false ;
}
return true ;
}
public static void main(String[] args)
{
int n = 128 ;
if (allDigitsDivide(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def checkDivisibility(n, digit) :
return (digit ! = 0 and n % digit = = 0 )
def allDigitsDivide( n) :
nlist = map ( int , set ( str (n)))
for digit in nlist :
if not (checkDivisibility(n, digit)) :
return False
return True
n = 128
print ( "Yes" if (allDigitsDivide(n)) else "No" )
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
static bool checkDivisibility( int n, int digit)
{
return (digit != 0 && n % digit == 0);
}
static bool allDigitsDivide( int n)
{
HashSet< char > nlist = new HashSet< char >(
Convert.ToString(n).ToCharArray());
foreach ( var digit in nlist)
{
if (checkDivisibility(n,
Convert.ToInt32(digit)))
return false ;
}
return true ;
}
public static void Main( string [] args)
{
int n = 128;
if (allDigitsDivide(n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
function checkDivisibility(n, digit){
return (digit != 0 && n % digit == 0);
}
function allDigitsDivide(n){
let nlist = new Set(n.toString());
nlist.forEach(digit => {
if (checkDivisibility(n, digit)){
return false ;
}
});
return true ;
}
let n = 128;
console.log((allDigitsDivide(n)) ? "Yes" : "No" );
|
Time Complexity: O(n), where n represents the given integer.
Auxiliary Space: O(n), where n represents the given integer.
Last Updated :
01 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...