DEV Community

vishwa v
vishwa v

Posted on

Python-6

Looping problems
1.Five students enter the classroom one after another. The first student gets roll number 1, the second gets 2, and so on. Display the assigned roll numbers.

student=1
while student<=5:
    print("Roll number:", student)
    student=student+1
Enter fullscreen mode Exit fullscreen mode

output:
Roll number: 1
Roll number: 2
Roll number: 3
Roll number: 4
Roll number: 5

2.A coach asks students to stand only at even-numbered positions in a queue. Display the first five positions.

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

output:
2
4
6
8
10

3.A newly constructed building has a special lift that does not stop on every floor. For safety reasons, it stops only at every third floor. A person enters the lift at the ground floor and presses the "Up" button.

Display the first five floors where the lift will stop

floor = 3
while floor <= 15:   # first five multiples of 3
    print("Lift stops at floor:", floor)
    floor += 3
Enter fullscreen mode Exit fullscreen mode

output:
Lift stops at floor: 3
Lift stops at floor: 6
Lift stops at floor: 9
Lift stops at floor: 12
Lift stops at floor: 15

4.A rocket launches with a countdown using only even numbers from 10. Display the countdown.

k=10
while k>=2:
    print(k)
    k-=2
Enter fullscreen mode Exit fullscreen mode

output:
10
8
6
4
2

Top comments (0)