Program to print all Disarium numbers between 0 and 100 in Python

Program to print all Disarium numbers between 0 and 100 in Python. This program is made of functions, while loop and for loop. This program is for beginners, class 11, 12 and B Tech Computer science students. Download souce code from below download button.

Program to print all Disarium numbers between 0 and 100 in Python

What is 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 becomes equal to the number itself.
For example, 175 is a Disarium number as follows:
11+ 72 + 53 = 1+ 49 + 125 = 175

Source code :

    
def calculateLength(n):    
    length = 0;    
    while(n != 0):    
        length = length + 1;    
        n = n//10;    
    return length;    
     
    
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;    
     
   
print("Disarium numbers between 1 and 100 are");    
for i in range(1, 101):    
    result = sumOfDigits(i);    
        
    if(result == i):    
        print(i), 

Step wise Source code Explanation:

Step 1 : 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;
Step 2 : 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;
Step 3 : 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 of Source Code :

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
 RESTART: C:/csstudy/download source code files/Program to check given number is a Disarium Number.py 
Disarium numbers between 1 and 100 are
1
2
3
4
5
6
7
8
9
89
>>> 

Output Explanation :

Disarium numbers between 1 and 100 are 1 2 3 4 5 6 7 8 9 89
These Disarium numbers are in the given range between 1 and 100.

Conclusion :

The Program is running fine.

Thank you for reading , learning and downloading the source code of the Program to print all Disarium numbers between 0 and 100 in Python post. This program is made of functions, while loop and for loop. This program is for beginners, class 11, 12 and B Tech Computer science students. For more programs click below.
Python Programs Archives – CS Study
Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study
Computer Science Practical file for class 12 Term-2 – CS Study

Leave a Comment

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