#Print even numbers from 1 to 10
for i in range(1, 11):
if i % 2 == 0:
print(i)
Output:
2
4
6
8
10
#Print squares of numbers from 1 to 5
for i in range(1, 6):
print(i**2)
Output:
1
4
9
16
25
#Print all numbers divisible by 3 between 1 and 20
for i in range(1, 21):
if i % 3 == 0:
print(i)
Output:
3
6
9
12
15
18
# Print elements of a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
# Sum of first 10 natural numbers
i=1
total = 0
while i <= 10:
total += i
i += 1
print("Sum:", total)
Output : Sum: 55
#Reverse a number
num = 1234
rev = 0
while num != 0:
rev = rev * 10 + num % 10
num //= 10
print("Reversed:", rev)
Output : Reversed:4321
# Factorial of a number using for loop
num = 5
fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial:", fact)
Output: Factorial: 120
# Count digits in a number using while loop
num = 12345
count = 0
while num != 0:
count += 1
num //= 10
print("Digits count:", count)
Output: Digits count: 5
#Find the largest number in a list using while loop
numbers = [5, 3, 8, 2, 9]
i=1
max_num = numbers[0]
while i < len(numbers):
if numbers[i] > max_num:
max_num = numbers[i]
i += 1
print("Maximum:", max_num)
Output: Maximum: 9
# Print numbers in triangle
for i in range(1, 6):
for j in range(1, i + 1):
print(j, end=" ")
print()
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#Count vowels in a string using for loop
text = "Hello World"
vowels = 'aeiouAEIOU'
count = 0
for char in text:
if char in vowels:
count += 1
print("Vowel count:", count)
Output: Vowel count: 3
#Find the average of numbers using while loop
numbers = [4, 8, 15, 16]
i=0
total = 0
while i < len(numbers):
total += numbers[i]
i += 1
average = total / len(numbers)
print("Average:", average)
Output: Average: 10.75
#Print all elements greater than 50 in a list
marks = [45, 67, 82, 49, 51]
for mark in marks:
if mark > 50:
print(mark)
Output:
67
82
51