Question
Print Disarium number in an array.
(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
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 arr[];
int num = 0, sum = 0, rem = 0, temp=0,len=0,size=0,i=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of Array:");
size=sc.nextInt();
arr=new int[size];
System.out.println("Enter Elements in Array:");
for(i=0;i< size;i++)
{
System.out.print((i+1)+":");
arr[i]=sc.nextInt();
}
System.out.println("Elements in Given Array:");
for(i=0;i< size;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
System.out.println("Disarium number in given Array:");
for(i=0;i< size;i++)
{
temp = arr[i];
sum=0;
// 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 = arr[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 == arr[i])
{
System.out.print(arr[i] + " ");
}
}
}
}
Python
size=int(input("Enter the size of List:"))
li=[]
print("Enter Elements in list:")
for i in range(0,size):
print((i+1),end=": ")
li.append(int(input()))
print("Element in Given List:")
print(li)
print("Disarium number in given List:")
for element in li:
temp=element
totalDigits=0
while(temp>0):
totalDigits=totalDigits+1
temp=temp//10
temp=element
sum=0
while(temp>0):
rem=temp%10
sum=sum+ rem**totalDigits
totalDigits = totalDigits - 1
temp=temp//10
if(sum==element):
print(element,end=" ")