Python Loops - While Loop

 With the while loop we can execute a set of statements as long as a condition is true.
while expression:
    statement(s)
Example: A program that adds number up to num where num is entered by the user.          The total = 1+2+3+4… up to the supplied number.

Program: Generate the following sequences using while loop:

  • Even Numbers Between 50 and 75
  • Numbers between 1 and 100 in reverse order that are divisible by 5

#A) Even Numbers Between 50 and 75
a=50
print("Even Numbers Between 50 and 75 is")
while(a<=75):
    if (a%2==0):
        print(a,end=' ')
    a+=1

Output:

Even Numbers Between 50 and 75 is 50 52 54 56 58 60 62 64 66 68 70 72 74

#B) Numbers between 1 and 100 in reverse order that are divisible by 5
i=100
print("Numbers between 1 and 100 in Reverse Order that are divisible by 5:"
while (i>=1):
  if (i%5==0):
    print (i, end = '  ')
  i = i - 5

Output:

Numbers between 1 and 100 in Reverse Order that are divisible by 5: 100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5

If you have any doubts, feel free to post it in comment Section.

Comments

Popular posts from this blog

Guessing the Number Game

Simple Arithmetic Calculator - User Defined Function

Find the final amount after Discount