Question
Program to determine whether a given number is a Disarium number in a given range.
(A number is said to be the Disarium number when the sum of its digit raised to the power of their respective positions is equal to the number itself.)
Enter the lower range
1
Enter the Upper range
500
Disarium Numbers:1 2 3 4 5 6 7 8 9 89 135 175
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.Scanner;
public class disariumNumberInRange
{
public static void main(String[] args)
{
int sum = 0, rem = 0, temp=0,i=0,upperRange=0,lowerRange=0,len=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the lower range");
lowerRange=sc.nextInt();
System.out.println("Enter the Upper range");
upperRange=sc.nextInt();
System.out.print("Disarium Numbers:");
for(i=lowerRange;i<=upperRange;i++)
{
temp=i;
while(temp>0)
{
len=len+1;
temp=temp/10;
}
sum=0;
/*Makes a copy of the original number num*/
temp = i;
/*Calculates the sum of digits powered with their respective position */
while(temp > 0)
{
rem = temp%10;
sum = sum + (int)Math.pow(rem,len);
temp = temp/10;
len--;
}
/*Checks whether the sum is equal to the number itself */
if(sum == i)
{
System.out.print(i + " ");
}
}
}
}
Python
lowerRange = int(input("Enter lower value:"))
upperRange = int(input("Enter upper value:"))
print("Disarium number between ", lowerRange, "and", upperRange, ":")
# counting total number of digits in the given number
for i in range(lowerRange, upperRange + 1):
temp = i
totalDigits = 0
while (temp > 0):
totalDigits = totalDigits + 1
temp = temp // 10
# calculating sum
temp = i
sumOfDigits = 0
while (temp > 0):
rem = temp % 10
sumOfDigits = sumOfDigits + rem ** totalDigits
totalDigits = totalDigits - 1
temp = temp // 10
if (sumOfDigits == i):
print(i, end=" ")