In this program, we need to display all Disarium number between 1 and 100.
A number is said to be a Disarium number when the sum of its digit raised to the power of their respective position is equal to the number itself.
For example, 175 is a Disarium number as follows
11 + 72 + 53 = 1 + 49 + 125 = 175
Some of the other example of Disarium number are 89, 135, 518 etc.
To find whether given number is Disarium or not, calculate the sum of digits powered with their respective positions. If the sum is equal to the original number then, the given number is Disarium number.
- calculateLength() counts the digits present in a number.
- Use a while loop to check whether the number is equal to 0 or not.
- Divide the number by 10 and increment the variable length by 1.
- Return length.
- sumOfDigits() calculates the sum of digits raised to their respective positions.
- Make a call to calculateLength() to get the number of digits present in given number and store the value in variable len.
- Using while loop calculate remainder rem repeatedly by dividing num with 10.
- Calculate the value of rem raised to power its position, i.e. remlen and store the computed value in a variable sum.
- To display all Disarium numbers between 1 and 100,
- Start a loop from 1 to 100, then make a call to sumOfDigits() method for each value from 1 to 100 and store the return value into a variable result.
- If the value of result is equal to number. It implies that given number is Disarium number. Hence, display it.