Disarium Number - GeeksforGeeks

07 Mar, 2022
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.
Examples: 
Input   : n = 135
Output  : Yes 
1^1 + 3^2 + 5^3 = 135
Therefore, 135 is a Disarium number

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

Input   : n = 80
Output  : No
8^1 + 0^2 = 8
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 digit count and decrement the digit count.
Below is the implementation of above idea. 
C++
Java
Python3
C#
Javascript
// C++ program to check whether a number is Disarium// or not#include<bits/stdc++.h>using namespace std; // Finds count of digits in nint 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 notbool 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);} //Driver code to check if number is disarium or notint main(){    int n = 135;    if( check(n))        cout << "Disarium Number";    else        cout << "Not a Disarium Number";    return 0;}
// Java program to check whether a number is disarium// or not class Test{    // Method to check whether a number is disarium or not    static boolean check(int n)    {        // Count digits in n.        int count_digits = Integer.toString(n).length();              // Compute sum of terms like digit multiplied by        // power of position        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 powering according to            // the positions            sum = (int) (sum + Math.pow(r, count_digits--));            x = x/10;        }              // If sum is same as number, then number is        return (sum == n);    }         // Driver method    public static void main(String[] args)    {        int n = 135;                 System.out.println(check(n) ? "Disarium Number" : "Not a Disarium Number");    }}
# Python program to check whether a number is Disarium# or notimport math # Method to check whether a number is disarium or notdef check(n) :     # Count digits in n.    count_digits = len(str(n))          # Compute sum of terms like digit multiplied by    # power of position    sum = 0  # Initialize sum of terms    x = n    while (x!=0) :         # Get the rightmost digit        r = x % 10                  # Sum the digits by powering according to        # the positions        sum = (int) (sum + math.pow(r, count_digits))        count_digits = count_digits - 1        x = x//10            # If sum is same as number, then number is    if sum == n :        return 1    else :        return 0       # Driver methodn = 135if (check(n) == 1) :    print ("Disarium Number")else :    print ("Not a Disarium Number")  # This code is contributed by Nikita Tiwari.
// C# program to check whether a number// is Disarium or notusing System; class GFG{     // Method to check whether a number// is disarium or notstatic bool check(int n){         // Count digits in n.    int count_digits = n.ToString().Length;      // Compute sum of terms like digit    // multiplied by power of position    // Initialize sum of terms    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 = (int)(sum + Math.Pow(              r, count_digits--));        x = x / 10;    }      // If sum is same as number,    // then number is    return (sum == n);} // Driver codepublic static void Main(string[] args){    int n = 135;         Console.Write(check(n) ? "Disarium Number" :                       "Not a Disarium Number");}} // This code is contributed by rutvik_56
<script> // JavaScript program to check whether a number is Disarium// or not// Method to check whether a number is disarium or notfunction check(n)    {             // Count digits in n.        var count_digits = Number.toString();              // Compute sum of terms like digit multiplied by        // power of position        var sum = 0; // Initialize sum of terms        var x = n;        while (x!=0)        {            // Get the rightmost digit            var r = x%10;                  // Sum the digits by powering according to            // the positions            sum = (sum + Math.pow(r, count_digits--));            x = x/10;        }              // If sum is same as number, then number is        return (sum = n);    }         // Driver method        var n = 135;                 document.write(check(n) ? "Disarium Number" : "Not a Disarium Number");         // This code is contributed by shivanisinghss2110</script>
Output: 
Disarium Number
YouTubeGeeksforGeeks502K subscribersDisarium Numbers | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 4:56•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=t4xdYNvjJyk" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Sahil Chhabra(KILLER). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
rutvik_56
arorakashish0911
shivanisinghss2110
amartyaghoshgfg
surinderdawra388
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to find GCD or HCF of two numbers
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find sum of elements in a given array
Program for factorial of a number
Program for Decimal to Binary Conversion
Sieve of Eratosthenes
Operators in C / C++
Euclidean algorithms (Basic and Extended)