TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to determine whether a given number is a Disarium number

Program to determine whether a given number is a Disarium number on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Program to determine whether a given number is a Disarium number

Explanation

In this program, we need to check whether the given number is Disarium or not.

Disarium number

A number is said to be the Disarium number when the sum of its digit raised to the power of their respective positions is equal to the number itself.

For example, 175 is a Disarium number as follows

11 + 72 + 53 = 1 + 49 + 125 = 175

Some of the other examples of Disarium number are 89, 135, 518 etc.

To find whether given number is Disarium or not, calculate the sum of digits powered with their respective positions. If the sum is equal to the original number then, the given number is Disarium number.

Algorithm

  1. calculateLength() counts the digits present in a number.
    1. Use a while loop to check whether the number is not equal to 0.
    2. Divide the number by 10 and increment the variable length by 1.
    3. Return length.
  2. Define and initialize variable num.
  3. Make a copy of num by storing the value of num in n.
  4. Using while loop calculates remainder rem repeatedly by dividing num with 10.
  5. Calculate the value of rem raised to power its position, i.e. remlen and store the computed value in a variable sum.
  6. Check whether sum is equal to number. If yes, then given number is Disarium number. Else, it is not a Disarium number.

Solution

Python

#calculateLength() will count the digits present in a number
def calculateLength(n):
    length = 0;
    while(n != 0):
        length = length + 1;
        n = n//10;
    return length;
 
num = 175;
rem = sum = 0;
len = calculateLength(num);
 
#Makes a copy of the original number num
n = num;
 
#Calculates the sum of digits powered with their respective position
while(num > 0):
    rem = num%10;
    sum = sum + int(rem**len);
    num = num//10;
    len = len - 1;
 
#Checks whether the sum is equal to the number itself
if(sum == n):
    print(str(n) + " is a disarium number");
else:
    print(str(n) + " is not a disarium number");

Output:

 175 is a disarium number

C

#include <stdio.h>
 
//calculateLength() will count the digits present in a number
int calculateLength(int n){
    int length = 0;
    while(n != 0){
        length = length + 1;
        n = n/10;
    }
    return length;
}
 
int main()
{
    int num = 175, sum = 0, rem = 0, n;
    int len = calculateLength(num);
    
    //Makes a copy of the original number num
    n = num;
    
    //Calculates the sum of digits powered with their respective position
    while(num > 0){
        rem = num%10;
        sum = sum + (int)pow(rem,len);
        num = num/10;
        len--;
    }
    
    //Checks whether the sum is equal to the number itself
    if(sum == n)
        printf("%d is a disarium number", n);
    else
        printf("%d is not a disarium number", n);
 
    return 0;
}

Output:

175 is a disarium number

JAVA

public class DisariumNumber
{
    //calculateLength() will count the digits present in a number
    public static int calculateLength(int n){
        int length = 0;
        while(n != 0){
            length = length + 1;
            n = n/10;
        }
        return length;
    }
    
    public static void main(String[] args) {
        int num = 175, sum = 0, rem = 0, n;
        int len = calculateLength(num);
        
        //Makes a copy of the original number num
        n = num;
        
        //Calculates the sum of digits powered with their respective position
        while(num > 0){
            rem = num%10;
            sum = sum + (int)Math.pow(rem,len);
            num = num/10;
            len--;
        }
        
        //Checks whether the sum is equal to the number itself
        if(sum == n)
            System.out.println(n + " is a disarium number");
        else
            System.out.println(n + " is not a disarium number");
    }
}

Output:

175 is a disarium number

C#

using System;                    
public class DisariumNumber
{
    //calculateLength() will count the digits present in a number
    public static int calculateLength(int n){
        int length = 0;
        while(n != 0){
            length = length + 1;
            n = n/10;
        }
        return length;
    }
    
    public static void Main()
    {
        int num = 175, sum = 0, rem = 0, n;
        int len = calculateLength(num);
        
        //Makes a copy of the original number num
        n = num;
        
        //Calculates the sum of digits powered with their respective position
        while(num > 0){
            rem = num%10;
            sum = sum + (int)Math.Pow(rem,len);
            num = num/10;
            len--;
        }
        
        //Checks whether the sum is equal to the number itself
        if(sum == n)
            Console.WriteLine(n + " is a disarium number");
        else
            Console.WriteLine(n + " is not a disarium number");
    }
}

Output:

175 is a disarium number

PHP

<!DOCTYPE html>
<html>
<body>
<?php
//calculateLength() will count the digits present in a number
function calculateLength($n){
    $length = 0;
    while($n != 0){
        $length = $length + 1;
        $n = intval($n/10);
    }
    return $length;
}
    
$num = 175;
$rem = $sum = 0;
$len = calculateLength($num);
    
//Makes a copy of the original number num
$n = $num;
 
//Calculates the sum of digits powered with their respective position
while($num > 0){
    $rem = $num%10;
    $sum = $sum + pow($rem,$len);
    $num = intval($num/10);
    $len--;
}
 
//Checks whether sum is equal to the number itself
if($sum == $n)
    print($n . " is a disarium number");
else
    print($n . " is not a disarium number");
?>
</body>
</html>

Output:

175 is a disarium number

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf