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
Here, updating the condition is important , otherwise the loop will execute infinitely.
Infinite Loop Example:
i=1
while i<=5:
print(i)
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 ")
Output:
28 is a Perfect Number
Explanation:
- check the number : If the number is less than or equal to zero , then return 0.
- Reducing the loop iteration : by dividing no by 2 , to reduce unnecessary loop iterations.
- Checking factor: If the no is divide by i , then add i to sum.
- Return sum : Return the sum after finding all the factors of no.
- 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")
Output:
9 is a Neon Number
Explanation:
- Square the number: square the given number and store it in square variable.
- Sum of digit: find the sum of digits of square variable.
- Return the sum : Return the sum after calculated the sum of digits.
- 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')
Output:
145 is a Strong Number
Explanation:
- Separating as individual digit: Separating the no into separate digits.
- Calculating factorial: Then calculating the factorial of each digit , return the result and the result is added to sum.
- Returning sum : After calculating the factorial for all the digits , the sum is returned.
- 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)