Question
Program to determine whether a given number is a Disarium number.
(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.)
11 + 72 + 53 = 1 + 49 + 125 = 175
Enter a number
135
135 is a disarium number
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 DisariumNumber
{
public static void main(String[] args)
{
int num = 0, sum = 0, rem = 0, temp=0,len=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
num=sc.nextInt();
temp = num;
// To find the length of the number
while(temp>0)
{
len= len + 1;
temp= temp/10;
}
/*Makes a copy of the original number num*/
temp = num;
/*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 == num)
{
System.out.println(num + " is a disarium number");
}
else
{
System.out.println(num + " is not a disarium number");
}
}
}
Python
number=int(input("Enter a number:"))
#counting total number of digits in the given number
temp=number
totalDigits=0
while(temp>0):
totalDigits=totalDigits+1
temp=temp//10
temp=number
sum=0
while(temp>0):
rem=temp%10
sum=sum+ rem**totalDigits
totalDigits = totalDigits - 1
temp=temp//10
if(sum==number):
print(number,"is a Disarium number")
else:
print(number,"is not a Disarium number")