Programming Golden Rules:
1) Never say "I don't know." Say "Let me try."
2) Known To Unknown
3) Don't Think about entire output
4) Think about very next step
5) Introduce a variable only when it simplifies the solution.
6) Micro To Macro
7) Dry Run Before You Run
8) Every Program Has Three Parts: Input, Process, Output
9) Logic First, Syntax Next
1) A teacher asks five students to stand in a line. Each student wears a badge with the number 1. Display the badge numbers from left to right.
student = 1
print(1, end = ' ')
student = student + 1 #student = 2
print(1, end = ' ')
student = student + 1 #student = 3
print(1, end = ' ')
student = student + 1 #student = 4
print(1, end = ' ')
student = student + 1 #student = 5
print(1, end = ' ')
student = student + 1 #student = 6
Output:
1 1 1 1 1
while loop:
student = 1
while student <=5:
student = student + 1
print(1, end = ' ')
Output:
1 1 1 1 1
2) 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.
while loop:
student = 0
while student <5:
student = student + 1
print(student, end = ' ')
Output:
1 2 3 4 5
3) A coach asks students to stand only at even-numbered positions in a queue. Display the first five positions.
count=1
position=0
while(count<6):
position=position+2
count=count+1
print(position)
Output:
2
4
6
8
10
4) 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.
count=1
floor=3
while(count<6):
floor=floor+3
count=count+1
print(floor )
*Output: *
6
9
12
15
18
5) A rocket launches with a countdown using only even numbers from 10. Display the countdown.
count=10
while count >= 2:
print(count)
count = count - 2
Output:
10
8
6
4
2
Top comments (0)