0% found this document useful (0 votes)
4 views6 pages

For Loop Challenges: 1-25 Solutions

cpe loop

Uploaded by

itel jy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

For Loop Challenges: 1-25 Solutions

cpe loop

Uploaded by

itel jy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

for i in range(2, 21, 2):

For Loop Challenges - Part 1 (1–25) print(i)


Answers
1. Print numbers 1 to 10 Output:
Code:
2
for i in range(1, 11): 4
print(i) 6
8
Output: 10
12
1 14
2 16
3 18
4 20
5
6
7
8 4. Print odd numbers 1 to 20
9 Code:
10
for i in range(1, 21, 2):
print(i)

2. Print numbers 10 to 1 Output:


Code:
1
for i in range(10, 0, -1): 3
print(i) 5
7
Output: 9
11
10 13
9 15
8 17
7 19
6
5
4
3 5. Print numbers 5 to 50 (step 5)
2 Code:
1
for i in range(5, 51, 5):
print(i)

3. Print even numbers 1 to 20 Output:


Code:
5 36
10 49
15 64
20 81
25 100
30
35
40 8. Print cubes of numbers 1 to 10
45 Code:
50
for i in range(1, 11):
print(i**3)
6. Print multiples of 7 up to 70
Output:
Code:
1
for i in range(7, 71, 7):
8
print(i)
27
Output: 64
125
7 216
14 343
21 512
28 729
35 1000
42
49
56
9. Print numbers divisible by 3 up to 30
63 Code:
70
for i in range(3, 31, 3):
print(i)
7. Print squares of numbers 1 to 10
Output:
Code:
3
for i in range(1, 11):
6
print(i*i)
9
Output: 12
15
1 18
4 21
9 24
16 27
25 30
10. Print numbers divisible by 5 up to 50 13. Sum of odd numbers 1 to 20
Code: Code:

for i in range(5, 51, 5): total = 0


print(i) for i in range(1, 21, 2):
total += i
Output: print(total)
5 Output:
10
15 100
20
25
30
14. Sum of multiples of 5 up to 50
35
Code:
40
45 total = 0
50 for i in range(5, 51, 5):
total += i
print(total)
11. Sum of numbers 1 to 10
Output:
Code:
275
total = 0
for i in range(1, 11):
total += i
print(total) 15. Product of numbers 1 to 5
(1*2*3*4*5)
Output: Code:

55 prod = 1
for i in range(1,6):
prod *= i
12. Sum of even numbers 1 to 20 print(prod)
Code:
Output:
total = 0
120
for i in range(2, 21, 2):
total += i
print(total)
16. Average of numbers 1 to 10
Output: Code:

110
total = 0 fact *= i
count = 0 print(fact)
for i in range(1,11):
total += i Output:
count += 1 120
print(total/count)

Output:
20. Factorials of numbers 1 to 10
5.5 Code:

for n in range(1, 11):


17. Sum of squares 1 to 5 fact = 1
Code: for i in range(1, n+1):
fact *= i
total = 0 print(n, '=>', fact)
for i in range(1,6):
total += i*i Output:
print(total) 1 => 1
Output: 2 => 2
3 => 6
55 4 => 24
5 => 120
6 => 720
7 => 5040
18. Sum of cubes 1 to 5
8 => 40320
Code:
9 => 362880
total = 0 10 => 3628800
for i in range(1,6):
total += i**3
print(total) 21. Multiplication table of 5
Code:
Output:
n = 5
225
for i in range(1,11):
print(f"{n} x {i} = {n*i}")

19. Factorial of a given number Output:


(example n=5)
5x1=5
Code:
5 x 2 = 10
n = 5 5 x 3 = 15
fact = 1 5 x 4 = 20
for i in range(1, n+1): 5 x 5 = 25
5 x 6 = 30
5 x 7 = 35 24. Division table of a given number
5 x 8 = 40 (example n=10, divide by 1..10)
5 x 9 = 45 Code:
5 x 10 = 50
n = 10
for i in range(1,11):
print(f"{n} / {i} = {n/i}")
22. Multiplication table of any given
number (example 7) Output:
Code:
10 / 1 = 10.0
n = 7 10 / 2 = 5.0
for i in range(1,11): 10 / 3 = 3.3333333333333335
print(f"{n} x {i} = {n*i}") 10 / 4 = 2.5
10 / 5 = 2.0
Output: 10 / 6 = 1.6666666666666667
7x1=7 10 / 7 = 1.4285714285714286
7 x 2 = 14 10 / 8 = 1.25
7 x 3 = 21 10 / 9 = 1.1111111111111112
7 x 4 = 28 10 / 10 = 1.0
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49 25. Modulo table (n % i for i=1..10,
7 x 8 = 56 example n=10)
7 x 9 = 63 Code:
7 x 10 = 70
n = 10
for i in range(1,11):
print(f"{n} % {i} = {n %
23. Multiplication tables 1 to 10 (nested i}")
loop)
Code: Output:

for n in range(1,11): 10 % 1 = 0
for i in range(1,11): 10 % 2 = 0
print(f"{n} x {i} = 10 % 3 = 1
{n*i}") 10 % 4 = 2
print() 10 % 5 = 0
10 % 6 = 4
Output: 10 % 7 = 3
10 % 8 = 2
See pattern: tables 1x1..1x10 then
10 % 9 = 1
2x1..2x10 etc.
10 % 10 = 0

You might also like