Program to check given number is a Disarium Number

Program to check given number is a Disarium Number in Python. This program contains while loop, functions, input and print statement. This program is for beginners, class 11,12 and B tech Computer science coding questions. Download source code.

Program to check given number is a Disarium Number

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

Algorithm :

  • STEP 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.
  • STEP 2: Define and initialize variable num.
  • STEP 3: Make a copy of num by storing the value of num in n.
  • STEP 4: Using while loop calculates remainder rem repeatedly by dividing num with 10.
  • STEP 5: Calculate the value of rem raised to power its position, i.e., remlen and store the computed value in a variable sum.
  • STEP 6: Check whether the sum is equal to the number. If yes, then given number is Disarium number. Else, it is not a Disarium number.

Source code(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 = int(input("enter the n.o to be checked"))    
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"); 

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;
num = int(input(“enter the n.o to be checked”))
rem = sum = 0;
len = calculateLength(num);
Step 2 :Makes a copy of the original number num
n = num;
Step 3 : 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;
Step 4 : 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 :

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 
enter the n.o to be checked175
175 is a disarium number
>>> 
 RESTART: C:/csstudy/download source code files/Program to check given number is a Disarium Number.py 
enter the n.o to be checked123
123 is not a disarium number
>>> 

Output Explanation:
Step 1 : N.o is captured
Enter the n.o to be checked 123
Step 2 : Calculation is done and result is printed on the computer screen.
123 is not a disarium number

Conclusion:

Program is running fine.

Thank you for reading, learning and downloading the source code of Program to check given number is a Disarium Number in Python. This program contains while loop, functions,input and print statement. This program is for begineers, class 11,12 and Btech Computer science coding questions. Download source code from above. 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.