java program for Disarium Number

Programming Example

Write a Program in Java to input a number and check whether it is a Disarium Number or not.

Note: A number will be called DISARIUM if sum of its digits powered with their respective position is equal to the original number.

For example 135 is a DISARIUM (Workings 11+32+53 = 135, some other DISARIUM are 89, 175, 518 etc)

Sample:

Enter a Number : 135

135 is a Disarium Number.

Enter a Number : 219

219 is not a Disarium Number.

Enter a Number : 89

89 is a Disarium Number.

Solution

import java.util.*;
class DisariumNumber
{
    public static void main(String arr[])
    {
        Scanner sc=new Scanner(System.in);
        int num,temp,sum=0,digit,count;
        System.out.println("Enter any Number");
        num=sc.nextInt();
        temp=num;
        String s=Integer.toString(num);
        count=s.length();
        while(temp>0)
        {
            digit=temp%10;
            sum=sum+(int)Math.pow(digit,count);
            temp=temp/10;
            count--;            
        }
        if(sum==num)
        {
            System.out.println("Number is Disarium Number");
        }
        else
        {
            System.out.println("Number is not a Disarium Number");
        }
    }
}
Output

Enter a Number : 135

135 is a Disarium Number.

-----------------------------------------------------------

Enter a Number : 219

219 is not a Disarium Number.

----------------------------------------------------------------

Enter a Number : 89 89 is a Disarium Number.

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Website development Company in Allahabad Sarkari Exam Quiz