class DisariumNo
{
static int calculateLength(int n)
{
int length = 0;
while(n != 0)
{
length = length + 1;
n = n/10;
}
return length;
}
static int pow(int n,int p)
{
int pw=1;
while(p>0)
{
pw=pw*n;
p--;
}
return pw;
}
static int sumOfDigits(int num)
{
int sum = 0, rem = 0;
int len = calculateLength(num);
while(num > 0)
{
rem = num%10;
sum = sum + pow(rem,len);
num = num/10;
len--;
}
return sum;
}
public static void main(String[] args)
{
int result = 0;
System.out.println("Disarium numbers between 1 and 100000 are\n");
for(int i = 1; i <= 100000; i++)
{
result = sumOfDigits(i);
if(result == i)
System.out.print(", " +i);
}
}
}
{
static int calculateLength(int n)
{
int length = 0;
while(n != 0)
{
length = length + 1;
n = n/10;
}
return length;
}
static int pow(int n,int p)
{
int pw=1;
while(p>0)
{
pw=pw*n;
p--;
}
return pw;
}
static int sumOfDigits(int num)
{
int sum = 0, rem = 0;
int len = calculateLength(num);
while(num > 0)
{
rem = num%10;
sum = sum + pow(rem,len);
num = num/10;
len--;
}
return sum;
}
public static void main(String[] args)
{
int result = 0;
System.out.println("Disarium numbers between 1 and 100000 are\n");
for(int i = 1; i <= 100000; i++)
{
result = sumOfDigits(i);
if(result == i)
System.out.print(", " +i);
}
}
}
No comments:
Post a Comment