TheDeveloperBlog.com

Home | Contact Us

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

Program to print all Disarium numbers between 1 to 100

Program to print all Disarium numbers between 1 to 100 on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Program to print all Disarium numbers between 1 and 100

Explanation

In this program, we need to display all Disarium number between 1 and 100.

Disarium number

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

For example, 175 is a Disarium number as follows

Formula

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

Some of the other example 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 equal to 0 or not.
    2. Divide the number by 10 and increment the variable length by 1.
    3. Return length.
  2. sumOfDigits() calculates the sum of digits raised to their respective positions.
    1. Make a call to calculateLength() to get the number of digits present in given number and store the value in variable len.
    2. Using while loop calculate remainder rem repeatedly by dividing num with 10.
    3. Calculate the value of rem raised to power its position, i.e. remlen and store the computed value in a variable sum.
  3. To display all Disarium numbers between 1 and 100,
    1. Start a loop from 1 to 100, then make a call to sumOfDigits() method for each value from 1 to 100 and store the return value into a variable result.
    2. If the value of result is equal to number. It implies that given number is Disarium number. Hence, display it.

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;
 
#sumOfDigits() will calculates the sum of digits powered with their respective position
def sumOfDigits(num):
    rem = sum = 0;
    len = calculateLength(num);
    
    while(num > 0):
        rem = num%10;
        sum = sum + (rem**len);
        num = num//10;
        len = len - 1;
    return sum;
  
result = 0;
 
#Displays all disarium numbers between 1 and 100
print("Disarium numbers between 1 and 100 are");
for i in range(1, 101):
    result = sumOfDigits(i);
    
    if(result == i):
        print(i),

Output:

Disarium numbers between 1 and 100 are
1 2 3 4 5 6 7 8 9 89 

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;
}
 
//sumOfDigits() will calculates the sum of digits powered with their respective position
int sumOfDigits(int num){
    int sum = 0, rem = 0;
    int len = calculateLength(num);
    
    while(num > 0){
        rem = num%10;
        sum = sum + pow(rem,len);
        num = num/10;
        len--;
    }
    return sum;
}
    
int main()
{
    int result = 0;
    
    //Displays all disarium numbers between 1 and 100
    printf("Disarium numbers between 1 and 100 are\n");
    for(int i = 1; i <= 100; i++){
        result = sumOfDigits(i);
    
        if(result == i)
            printf("%d ", i);
    }
 
    return 0;
}

Output:

Disarium numbers between 1 and 100 are
1 2 3 4 5 6 7 8 9 89

JAVA

public class DisariumNumbers
{
    //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;
    }
    
    //sumOfDigits() will calculates the sum of digits powered with their respective position
    public static int sumOfDigits(int num){
        int sum = 0, rem = 0;
        int len = calculateLength(num);
        
        while(num > 0){
            rem = num%10;
            sum = sum + (int)Math.pow(rem,len);
            num = num/10;
            len--;
        }
        return sum;
    }
    
    public static void main(String[] args) {
        int result = 0;
        
        //Displays all disarium numbers between 1 and 100
        System.out.println("Disarium numbers between 1 and 100 are");
        for(int i = 1; i <= 100; i++){
            result = sumOfDigits(i);
        
            if(result == i)
                System.out.print(i + " ");
        }
    }
}

Output:

Disarium numbers between 1 and 100 are
1 2 3 4 5 6 7 8 9 89 

C#

using System;
                    
public class DisariumNumbers
{
    //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;
    }
    
    //sumOfDigits() will calculates the sum of digits powered with their respective position
    public static int sumOfDigits(int num){
        int sum = 0, rem = 0;
        int len = calculateLength(num);
        
        while(num > 0){
            rem = num%10;
            sum = sum + (int)Math.Pow(rem,len);
            num = num/10;
            len--;
        }
        return sum;
    }
    
    public static void Main()
    {
        int result = 0;
        
        //Displays all disarium numbers between 1 and 100
        Console.WriteLine("Disarium numbers between 1 and 100 are");
        for(int i = 1; i <= 100; i++){
            result = sumOfDigits(i);
        
            if(result == i)
                Console.Write(i + " ");
        }
    }
}

Output:

Disarium numbers between 1 and 100 are
1 2 3 4 5 6 7 8 9 89 

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;
}
 
//sumOfDigits() will calculates the sum of digits powered with their respective position
function sumOfDigits($num){
    $rem = $sum = 0;
    $len = calculateLength($num);
 
    while($num > 0){
        $rem = $num%10;
        $sum = $sum + pow($rem,$len);
        $num = intval($num/10);
        $len--;
    }
    return $sum;
}
    
$result = 0;
 
//Displays all disarium numbers between 1 and 100
print("Disarium numbers between 1 and 100 are <br>");
for($i = 1; $i <= 100; $i++){
    $result = sumOfDigits($i);
 
    if($result == $i)
        print($i . " ");
}
?>
</body>
</html>

Output:

Disarium numbers between 1 and 100 are
1 2 3 4 5 6 7 8 9 89

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