Check whether a given number N is a Nude Number or not
Given an integer N, the task is to check whether N is a Nude number or not.
A Nude number is a number that is divisible by all of its digits (which should be nonzero).
Example:
Input: N = 672
Output: Yes
Explanation:
Since, 672 is divisible by all of its three digits 6, 7 and 2. Therefore the output is Yes.
Input: N = 450
Output: No
Explanation:
Since, 450 is not divisible by 0 (Also it gives exception). Therefore the output is No.
Approach: Extract digits from number one by one and check these two conditions:
- The digit must be non-zero, to avoid exception
- And the digit must divide the number N
When both these condition is satisfied by all digits of N, then the number is called Nude number.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
bool checkDivisbility( int num)
{
int digit;
int N = num;
while (num != 0) {
digit = num % 10;
num = num / 10;
if (digit == 0 || N % digit != 0)
return false ;
}
return true ;
}
int main()
{
int N = 128;
bool result = checkDivisbility(N);
if (result)
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean checkDivisbility( int num)
{
int digit;
int N = num;
while (num != 0 )
{
digit = num % 10 ;
num = num / 10 ;
if (digit == 0 || N % digit != 0 )
return false ;
}
return true ;
}
public static void main(String[] args)
{
int N = 128 ;
boolean result = checkDivisbility(N);
if (result)
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def checkDivisbility(num):
digit = 0
N = num
while (num ! = 0 ):
digit = num % 10
num = num / / 10
if (digit = = 0 or N % digit ! = 0 ):
return False
return True
if __name__ = = '__main__' :
N = 128
result = checkDivisbility(N)
if (result):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool checkDivisbility( int num)
{
int digit;
int N = num;
while (num != 0)
{
digit = num % 10;
num = num / 10;
if (digit == 0 || N % digit != 0)
return false ;
}
return true ;
}
public static void Main()
{
int N = 128;
bool result = checkDivisbility(N);
if (result)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function checkDivisbility(num)
{
let digit;
let N = num;
while (num != 0) {
digit = num % 10;
num = Math.floor(num / 10);
if (digit == 0 || N % digit != 0)
return false ;
}
return true ;
}
let N = 128;
let result = checkDivisbility(N);
if (result)
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time complexity: O( log10 N ), where N is the given integer.
Auxiliary space: O(1) as constant space is being used.
Last Updated :
22 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...