DEV Community

Ajay
Ajay

Posted on

Python learning Series | while loop program ..

1) Prefect number

  • A perfect number is a positive integer that equals the sum of its positive proper divisors (divisors smaller than the number itself).
    Eg

  • Number is 6

  • Divisor for number 6 is 1 , 2 ,3

  • sum of the divisor(1+2+3 = 6) is equal to the given number are same then , it is prefect number.

This approach works for sum of digits in single digits.

def divisors(no) :
    div = 1
    divisor = 0
    while div <= no //2 :
        if no % div == 0 :
         divisor = (divisor  *  10) + div
         print(divisor)
        div += 1
    return divisor

def sum_of_digits(value):
    total = 0
    while value > 0:
        total = total + value % 10
        value = value // 10
    return total


number = 6
divisor =divisors(number)
print(divisor)
result = sum_of_digits(divisor)
print(result)
if(number == result):
    print("prefect number")
else :
    print("Not prefect number")

Enter fullscreen mode Exit fullscreen mode

This works for all digits

def prefect_number(no):
    div = 1
    total = 0
    while div <= no //2:
        if no % div == 0 :
            total = total + div
        div += 1
    return total
num = 6
result = prefect_number(num)
if num == result :
    print("prefect")
else:
    print("not prefect")
Enter fullscreen mode Exit fullscreen mode

2) Neon number

A neon number is a number where the sum of the digits of its square equals the original number
*Eg *

  • number 9
  • square of this number is 81
  • sum of the this square is 9
  • Therefore input is equal to output ,so it is neon number
def neon_number(no):
    square = no ** 2
    total = 0
    while square > 0 :
        total = total + square % 10
        square = square // 10
    return total
no = 2
result = neon_number(no)
if(result == no):
    print("It is Neon number")
else:
    print("Not Neon number")
Enter fullscreen mode Exit fullscreen mode

3) Strong Number

Strong Numbers are the numbers whose sum of factorial of digits is equal to the original number.
Eg:

  • Number is 145
  • check whether until number greater than 0 . if number less than or equal to 0 stop the loop.
  • Then split the number into single digits ( 5 , 4, 1 ).
  • find each digits factorial and sum of them.
  • check whether sum of the factorial is equal to the given number ,if true means given number is strong number else not .

Module Approach

no = 145
total = 0
while no > 0:
    split = no % 10

    fact = 1
    count = 1

    while count <= split :
        fact = fact * count
        count += 1
    total = total + fact
    no =  no // 10

print(total)
Enter fullscreen mode Exit fullscreen mode

Functional Approach

def strong_number(no):
    total = 0
    while no > 0 :
        split = no % 10 

        fact = 1
        count = 1

        while count <= split :
            fact = fact * count
            count += 1
        total = total + fact
        no = no // 10
    return total

no = 145
result = strong_number(no)
if(no == result):
    print("Strong Number")
else :
    print("Not Strong Number")
Enter fullscreen mode Exit fullscreen mode

### 4) Automorphic Number
An automorphic number is an integer whose square ends with the exact same digits as the number itself.

Eg

  • Number is 16
  • Using power operator multiple the number
  • check wheather num greather than 0.
  • get given numbers's last digits and squared number's last digits
  • if both matchs print Automorphic Number else not.
def automorphic_number(no) :
    num = no ** 2
    while num > 0 :
        last = num % 10
        temp = no % 10
        if(last == temp ):
            return True
        else:
            return False
result = automorphic_number(16)
if(result == True ) :
    print("automorphic_number")
else:
    print("Not automorphic_number")
Enter fullscreen mode Exit fullscreen mode

5) Duct Number

A duck number is a positive number that contains at least one zero (0), but does not start with a zero.

Eg

  • Given number = 1102
  • check the while loop until given number become 0.
  • get its digits one by one no % 10
  • check the digits is equal to 0 , if 0 means it is duct number.
  • else means loop it to last digits is not equal to zero means that is not duck number.

Bodule Approach

#no = 034
#while no > 0 :
#    isZero = no % 10
#    if(isZero == 0 ) :
#        print("is Duct Number")
#        break
#    no = no // 10
#else :
#    print("not duct Number")
Enter fullscreen mode Exit fullscreen mode

Functional Approach

def duck_number(no):

    while no > 0:
        isZero = no % 10
        if(isZero == 0) :
            return True
        no = no // 10
    else :
            return False


num = 101
result = duck_number(num)
if(result == True):
    print("is Duct Number")
else :
     print("Not Duct Number")
Enter fullscreen mode Exit fullscreen mode

Note : In python there is no possible integer to start with zero except octal integers because,
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers

6) Spy Number

The number is said to be a Spy number if the sum of all the digits is equal to the product of all digits.

Eg

  • Given number is 1414
  • Declared two variable for sum and product
  • Sum the digits using** sum = sum + no % 10**
  • product the digits using product = product * no % 10
  • loop until the no becomes less then zero
  • then check the sum and product of numbe which is equal print spy number else not.

Module Approach

no = 1412

sum_digits = 0
product_digits =1
while no > 0:
    sum_digits = sum_digits + no % 10
    product_digits = product_digits * no % 10
    no = no//10

print(sum_digits)
print(product_digits)
if(sum_digits == product_digits):
    print("Is Spy number")
else :
    print("Not Spy Number")
Enter fullscreen mode Exit fullscreen mode

Functional Approach

def spy_number(no):

    sum_digits = 0
    product_digits =1
    while no > 0:
        sum_digits = sum_digits + no % 10
        product_digits = product_digits * no % 10
        no = no // 10

    return sum_digits , product_digits

num = 1111
sumof , productof = spy_number(num)
if(sumof == productof):
    print("Spy Number")
else :
    print("Not Spy number")
Enter fullscreen mode Exit fullscreen mode

7) Amicable Number

Amicable numbers are two distinct natural numbers where the sum of the proper divisors (all positive divisors except the number itself) of each number equals the other number.

  • The sum of the proper divisors of (a) equals (b).
  • The sum of the proper divisors of (b) equals (a).

Functional Approach

def divisor(no):
    div = 1
    total = 0
    while div <= no //2:
        if no % div == 0:
            total = total + div
        div +=1
    return total

no1 = 48
no2 = 75
num1 = divisor(no1)
num2 = divisor(no2)

if(no1 == num2 and no2 == num1):
    print("IS Amicable Number")
else:
    print("Not Amicable number")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)