Disarium Number - GeeksforGeeks
Given a number "n", find if it is Disarium or not. A number is called Disarium if sum of its digits powered with their respective positions is equal to the number itself.
Read full article from Disarium Number - GeeksforGeeks
Given a number "n", find if it is Disarium or not. A number is called Disarium if sum of its digits powered with their respective positions is equal to the number itself.
int
countDigits(
int
n)
{
int
count_digits = 0;
// Count number of digits in n
int
x = n;
while
(x)
{
x = x/10;
// Count the no. of digits
count_digits++;
}
return
count_digits;
}
// Function to check whether a number is disarium or not
bool
check(
int
n)
{
// Count digits in n.
int
count_digits = countDigits(n);
// Compute sum of terms like digit multiplied by
// power of position
int
sum = 0;
// Initialize sum of terms
int
x = n;
while
(x)
{
// Get the rightmost digit
int
r = x%10;
// Sum the digits by powering according to
// the positions
sum = sum +
pow
(r, count_digits--);
x = x/10;
}
// If sum is same as number, then number is
return
(sum == n);
}