Print all possible positive integers between 1 and 1000 (inclusive) such that:
- There is no duplicate digits.
- There is no zero in the digits.
- The number is divisible by each of it's digits.
SAMPLE INPUT SAMPLE OUTPUT
1 2 3 4 5 6 7 8 9 12 15 24 36 48 124 126 128 132
135 162 168 175 184 216 248 264 312 315 324 384
396 412 432 612 624 648 672 728 735 784 816 824
864 936
Time Limit: 5.0 seconds (s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
//Program start Here
import java.util.*;
class TestClass
{
public static void main(String args[] ) throws Exception
{
TestClass h1=new TestClass();
String output="";
for (int i = 1; i < 1000; i++)
{
if(h1.check_no_duplicate_digit(i) && h1.check_no_zeros(i) && h1.check_divisible_by_each_digit(i))
output +=i+" ";
}
System.out.println(output);
}
boolean check_no_duplicate_digit(int val)
{
boolean flag=true;
String value=val+"";
String output="";
int count=0;
for (int i = 0; i < value.length(); i++)
{
if(output.indexOf(value.charAt(i)) == -1)
{
output+=value.charAt(i);
}
else
{
flag=false;
break;
}
}
return flag;
}
boolean check_no_zeros(int val)
{
boolean flag=true;
if(val%10==0)
flag=false;
return flag;
}
boolean check_divisible_by_each_digit(int val)
{
boolean flag=true;
String output=val+"";
for (int i = 0; i < output.length(); i++)
{
int digit=Integer.parseInt(output.charAt(i)+"");
if(digit==0)
{
flag=false;
break;
}else if(val%digit!=0)
flag=false;
}
return flag;
}
}
Comments
Post a Comment