A number is said to be disarium number if the sum of its digits powered with their position is the same as the original number.
For example, if the number is 135, then the sum of the digits powered with their position can be calculated as:
11+32+53 (position) = 1+9+125 (power) = 135, this is equal to the original number. Therefore, 135 is a disarium number.
If the number is 125, then the sum of the digits powered with their position is:
11+22+53 = 1+4+125 = 130, this is not equal to the original number. Therefore, 125 is not a disarium number.
For this program, a number is taken as input. Then the number of digits of the number is counted and according to the position the digits are powered and added. After completing, the sum of the power of the digits are compared with the original number, if both are the same, then the number is a disarium number, otherwise not.
INPUT: A number
OUTPUT: Whether the number is a disarium number or not
PROCESS:
Step 1: [Taking the input]
Read n [number to be checked]
Step 2: [Checking for disarium number]
Set d<-0
Set s<-0
[Counting the number of digits of the given number]
Set tmp<-n
While tmp>0 repeat
Set d<-d+1
Set tmp<-tmp/10
[End of ‘while’ loop]
[Finding the sum of the power of digits]
Set tmp<-n
While tmp>0 repeat
[Power is calculated according the position of the digits]
Set s<-s+(tmp mod 10)d
Set tmp<-tmp/10
Set d<-d-1
[End of ‘while’ loop]
[Checking the condition for disarium number]
If s=n then
Print "The given number is a disarium number"
Else
Print "The given number is not a disarium number"
[End of ‘if-else’]
Step 3: Stop.
while(tmp>0)--------------------------------------O(n) [‘n’ is the number of digits]
{ d++;
tmp/=10; }
//finding the sum of the power of digits
tmp=n;
while(tmp>0)--------------------------------------O(n) [‘n’ is the number of digits]
{ //power is calculated according the position
//of the digits
s=s+pow(tmp%10,d);
tmp/=10;
d--; }
//checking the condition for disarium number
if(s==n)---------------------------------------------O(1)
printf("The given number is a disarium number");
else-------------------------------------------------O(1)
printf("The given number is not a disarium number");
The time complexity of this program is O(n) where ‘n’ is the number of digits of the number to be checked.
The space complexity of this program is O(1) as it requires a constant number of spaces to execute for any given input.