This homework will help you practise for loop, while loop, range(), break and continue.
1. Print numbers from 1 to 10
Hint: Use
for loop with range().for i in range(1, 11):
print(i)
2. Print even numbers from 1 to 20
Hint: Use step value in
range().for i in range(2, 21, 2):
print(i)
3. Print "Hello" five times using while loop
Hint: Use a counter variable.
count = 1
while count <= 5:
print("Hello")
count += 1
4. Find the sum of numbers from 1 to 10
Hint: Start sum with 0 and add inside loop.
total = 0
for i in range(1, 11):
total += i
print("Sum:", total)
5. Display multiplication table of a given number
Hint: Loop from 1 to 10.
n = int(input("Enter a number: "))
for i in range(1, 11):
print(n, "x", i, "=", n * i)
6. Print numbers entered by user until user enters 0
Hint: Use condition based
while loop.n = int(input("Enter number: "))
while n != 0:
print(n)
n = int(input("Enter number: "))
7. Stop the loop when number is 5
Hint: Use
break keyword.for i in range(1, 10):
if i == 5:
break
print(i)
8. Skip printing number 3
Hint: Use
continue.for i in range(1, 6):
if i == 3:
continue
print(i)
9. Count number of digits in a number
Hint: Use
while loop and // operator.n = int(input("Enter number: "))
count = 0
while n > 0:
count += 1
n //= 10
print("Number of digits:", count)
10. Find factorial of a number
Hint: Multiply numbers from 1 to n.
n = int(input("Enter number: "))
fact = 1
for i in range(1, n + 1):
fact *= i
print("Factorial:", fact)
Note to Students:
- Type the programs instead of copying.
- Change values and observe output.
- Understand loop flow before memorizing.
0 Comments