Disarium number

Disarium number can be defined as a number whose sum of digits, powered with their respective position, is equal to the original number.

Write a program to determine given number is disarium number.

For example

Input:
135

Output
Disarium number

1*1 + 3*2 + 5*3 = 135

Solution:

using System;

namespace DisarumNUmber
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Enter a number:");
            var num = Convert.ToInt32(Console.ReadLine());
            var length = 0;
            int on;
            var n = on = num;
            while (n != 0)
            {
                length += 1;
                n /= 10;
            }

            var sum = 0;
            while (num > 0)
            {
                var rem = num % 10;
                sum = sum + (int)Math.Pow(rem, length);
                num = num / 10;
                length--;
            }

            Console.WriteLine(sum == on ? "Disarium number" : "Not a disarium number");
            Console.ReadLine();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *