DEV Community

Cover image for While loop in python
Rakshambika
Rakshambika

Posted on

While loop in python

What is while loop ?

  • While is used to execute a block of code until a specified condition is true.

Syntax:

while condition:
      //block of code
      //update condition
Enter fullscreen mode Exit fullscreen mode

Here, updating the condition is important , otherwise the loop will execute infinitely.

Infinite Loop Example:

i=1
while i<=5:
      print(i)
Enter fullscreen mode Exit fullscreen mode

Here, i is never incremented so condition the is always true.
This cause infinite loop.


Practice programs:

1.Perfect Number :

  • A number is said to be perfect if sum of all its factors excluding the number itself is equal to the number.
  • Ex: 6 (1+2+3 => 6) , 28 (1+2+4+7+14 =>28)

Code:

def perfect_no(no) :
          if no <= 1:   
             return 0 

          temp = no // 2
          i=1
          sum=0

          while temp>=i:
             if no % i == 0 :
                sum += i
             i +=1

          return sum


no=28      
result=perfect_no(no)
if result == no :
   print(no," is a Perfect Number ")
else:
   print(no," is Not a Perfect Number ")
Enter fullscreen mode Exit fullscreen mode

Output:

28 is a Perfect Number
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. check the number : If the number is less than or equal to zero , then return 0.
  2. Reducing the loop iteration : by dividing no by 2 , to reduce unnecessary loop iterations.
  3. Checking factor: If the no is divide by i , then add i to sum.
  4. Return sum : Return the sum after finding all the factors of no.
  5. Check perfect or not: Comparing the returned value with no , if both are equal then it is a perfect number otherwise not.

2. Neon Number :

  • A neon number is a number where the sum of the digits of its square equals the original number
  • Ex: 9
  • Square of 9 = 81
  • sum of digits of its square = 9

Code:

def neon(no):
    square=no*no
    sum=0

    while square>0:
          sum = sum + square % 10
          square = square // 10

    return sum


no=9
result=neon(no)
if result == no:
   print(no , " is a Neon Number")
else:
   print(no, " is not a Neon Number")
Enter fullscreen mode Exit fullscreen mode

Output:

9  is a Neon Number
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Square the number: square the given number and store it in square variable.
  2. Sum of digit: find the sum of digits of square variable.
  3. Return the sum : Return the sum after calculated the sum of digits.
  4. Check neon or not: Comparing the returned value with no , if both are equal then it is a neon number otherwise not.

Strong Number :

  • A strong number is a positive integer where the sum of the factorials of its individual digits equals the original number itself.
  • Ex: 145 is a strong number because 1! + 4! + 5! = 1 + 24 + 120 = 145.

Code:

def factorial(no):
    i=1
    fact=1

    while i <= no :
          fact *= i
          i += 1

    return fact


def strong(no):
    sum=0

    while no>0:
          temp = no % 10
          no = no // 10
          fact=factorial(temp)
          sum += fact

    return sum



number=145
result=strong(number)

if number == result:
   print(number , ' is a Strong Number')
else:
   print(number , ' is not a Strong Number')

Enter fullscreen mode Exit fullscreen mode

Output:

145  is a Strong Number
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Separating as individual digit: Separating the no into separate digits.
  2. Calculating factorial: Then calculating the factorial of each digit , return the result and the result is added to sum.
  3. Returning sum : After calculating the factorial for all the digits , the sum is returned.
  4. Check strong or not: Comparing the returned value with no , if both are equal then it is a strong number otherwise not.

Top comments (0)