Python Program - Iteration Statements
1. Program using 'for' loop
Aim:
To print the first 10 natural numbers using a for loop.
Algorithm:
1. Start the program.
2. Use a for loop to iterate from 1 to 10.
3. Print each number during iteration.
4. End.
Program:
for i in range(1, 11):
print(i)
2. Program using 'while' loop
Aim:
To print numbers from 1 to 10 using a while loop.
Algorithm:
1. Start the program.
2. Initialize a variable i = 1.
3. Use a while loop with the condition i <= 10.
4. Print the value of i and increment it.
5. End.
Program:
i=1
while i <= 10:
print(i)
i += 1
3. Program to print even numbers using loop
Aim:
To print all even numbers between 1 and 20 using a loop.
Algorithm:
1. Start the program.
2. Use a for loop to iterate from 1 to 20.
3. Check if the number is even using if (number % 2 == 0).
4. Print the number if it is even.
5. End.
Program:
for num in range(1, 21):
if num % 2 == 0:
print(num)