Open In App

Disarium Number

Last Updated : 05 Feb, 2025
Comments
Improve
Suggest changes
4 Likes
Like
Report

Given a number "n", The task is to 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.

Examples: 

Input: n = 135
Output: Yes
Explanation: 1^1 + 3^2 + 5^3 = 135
Therefore, 135 is a Disarium number

Input: n = 89
Output: Yes
Explanation: 8^1+9^2 = 89
Therefore, 89 is a Disarium number

Input: n = 80
Output: No
Explanation: 8^1 + 0^2 = 8

Approach:

The idea is to first count digits in given numbers. Once we have count, we traverse all digits from right most (using % operator), raise its power to count and decrement the count.

C++
// C++ program to check whether a number is Disarium
// or not
#include <iostream>
#include <cmath>
using namespace std;

// Finds count of digits in n
int countDigits(int n) {
    int count = 0;

    // Count number of digits in n
    int x = n;
    while (x) {
        x = x / 10;

        // Count the no. of digits
        count++;
    }
    return count;
}

// Function to check whether a number is disarium or not
bool isDisarium(int n) {
  
    // Count digits in n.
    int count = countDigits(n);

    // Compute sum of terms like digit multiplied by
    // power of position
  
   // Initialize sum of terms
    int sum = 0; 
    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--);
        x = x / 10;
    }

    // If sum is same as number, then number is
    return (sum == n);
}

int main() {
    int n = 135;
    if (isDisarium(n))
        cout << "True";
    else
        cout << "False";
    return 0;
}
Java
// Java program to check whether a number is disarium
// or not

// Java program to check whether a number is Disarium or not
 class GfG {

    // Function to count the number of digits in n
    static int countDigits(int n) {
        int count = 0;
        int x = n;

        // Count the number of digits in n
        while (x > 0) {
            x = x / 10;
            count++;
        }
        return count;
    }

    // Function to check whether a number is Disarium or not
    static boolean isDisarium(int n) {
        // Count digits in n
        int count = countDigits(n);

        // Compute sum of digits raised to the power of their positions
        int sum = 0; // Initialize sum of terms
        int x = n;
        while (x > 0) {
            // Get the rightmost digit
            int r = x % 10;

            // Sum the digits by raising them to the power of their positions
            sum += Math.pow(r, count--);
            x = x / 10;
        }

        // If sum is same as the number, then it is a Disarium number
        return (sum == n);
    }

    public static void main(String[] args) {
        int n = 135;
        if (isDisarium(n))
            System.out.println("True");
        else
            System.out.println("False");
    }
}
Python
# Python program to check whether a number is Disarium
# or not

# Finds count of digits in n
def countDigits(n):
    count = 0

    # Count number of digits in n
    x = n
    while x:
        x = x // 10  # Integer division

        # Count the no. of digits
        count += 1
    return count

# Function to check whether a number is disarium or not
def isDisarium(n):
  
    # Count digits in n.
    count = countDigits(n)

    # Compute sum of terms like digit multiplied by
    # power of position
    sum = 0  
    x = n
    while x:
        # Get the rightmost digit
        r = x % 10

        # Sum the digits by powering according to
        # the positions
        sum = sum + pow(r, count)
        count -= 1
        
        # Integer division
        x = x // 10  

    # If sum is same as number, then number is
    return (sum == n)
  
if __name__ == "__main__":
    n = 135
    if isDisarium(n):
        print("True")
    else:
        print("False")
C#
// C# program to check whether a number is Disarium
// or not
using System;

class GfG
{
    // Finds count of digits in n
    static int countDigits(int n)
    {
        int count = 0;

        // Count number of digits in n
        int x = n;
        while (x > 0)
        {
            x = x / 10;

            // Count the no. of digits
            count++;
        }
        return count;
    }

    // Function to check whether a number is disarium or not
    static bool isDisarium(int n)
    {
        // Count digits in n.
        int count = countDigits(n);

        // Compute sum of terms like digit multiplied by
        // power of position
        int sum = 0; 
        int x = n;
        while (x > 0)
        {
            // Get the rightmost digit
            int r = x % 10;

            // Sum the digits by powering according to
            // the positions
            sum = sum + (int)Math.Pow(r, count--);
            x = x / 10;
        }

        // If sum is same as number, then number is
        return (sum == n);
    }

    static void Main()
    {
        int n = 135;
        if (isDisarium(n))
            Console.WriteLine("True");
        else
            Console.WriteLine("False");
    }
}
JavaScript
// Finds count of digits in n
function countDigits(n) {
    let count = 0;

    // Count number of digits in n
    let x = n;
    while (x > 0) {
        x = Math.floor(x / 10); 

        // Count the no. of digits
        count++;
    }
    return count;
}

// Function to check whether a number is disarium or not
function isDisarium(n) {

    // Count digits in n.
    let count = countDigits(n);

    // Compute sum of terms like digit multiplied by
    // power of position
    let sum = 0;
    let x = n;
    while (x > 0) {
    
        // Get the rightmost digit
        let r = x % 10;

        // Sum the digits by powering according to
        // the positions
        sum = sum + Math.pow(r, count--);
        x = Math.floor(x / 10); // Integer division
    }

    // If sum is same as number, then number is
    return sum === n;
}

// Driver code
(function () {
    let n = 135;
    if (isDisarium(n))
        console.log("True");
    else
        console.log("False");
})();

Output
True

Time complexity : O(logn) 
Auxiliary Space : O(1)


 


Python Program to Check a Number is Disarium Number
Article Tags :

Explore