top of page

Python Number Programs

1) Python program to check if the given number is a 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.

  • Use a while loop to check whether the number is not equal to 0.

  • Divide the number by 10 and increment the variable length by 1.

  • 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.

​

back.png

#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");  

2) Python program to print all disarium numbers between 1 and 100

​

In this program, we need to print all disarium numbers between 1 and 100 by following the algorithm as given below:

​

ALGORITHM:

​

  • STEP 1: CalculateLength() counts the digits present in a number.

    • Use a while loop to check whether the number is equal to 0 or not.

    • Divide the number by 10 and increment the variable length by 1.

    • Return length.

  • STEP 2: sumOfDigits() calculates the sum of digits raised to their respective positions.

    • Make a call to calculateLength() to get the number of digits present in a given number and store the value in variable len.

    • Using while loop calculates remainder rem repeatedly by dividing num with 10.

    • Calculate the value of rem raised to power its position, i.e., remlen and store the computed value in a variable sum.

  • STEP 3: To display all Disarium numbers between 1 and 100,

    • 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.

    • If the value of the result is equal to the number, it implies that the given number is Disarium number. Hence, display it.

back.png

#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),    

bottom of page