-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest3.py
More file actions
49 lines (42 loc) · 1.45 KB
/
test3.py
File metadata and controls
49 lines (42 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#using for and range(start, stop, step) function to print numbers and multiplication table
# Code to print numbers from 1 to 100
for el in range(1, 101):
print(el)
print("Finished printing numbers from 1 to 100")
# Code to print numbers from 100 to 1
for el in range(100, 0, -1):
print(el)
print("Finished printing numbers from 100 to 1")
# code to print multiplication table of a number
multiplication_table = int(input("Enter the number: "))
for el in range(1, 11):
print(multiplication_table, "x", el, "=", multiplication_table * el)
print("Finished multiplication table of number", multiplication_table)
#pass statement is used to skip the execution of a block of code.
# It is often used as a placeholder when you want to write code later or when you want to skip a certain condition in a loop or an if statement.
for el in range(1, 11):
if el == 5:
pass
else:
print(el)
print("Finished printing numbers from 1 to 10 with pass statement")
# sum of n numbers using while loop
sum = 0
n = 15
i = 1
while i <= n:
sum += i
i+=1
print("Sum of first", n, "numbers is:", sum)
# sum of n numbers using for loop: sum of first 5 numbers is 1+2+3+4+5 = 15
sum = 0
n = 5
for el in range(1, n+1):
sum += el
print("Sum of first", n, "numbers is:", sum)
# factorial of n numbers using for loop: factorial 5 is 5*4*3*2*1 = 120
factorial = 1
n = 5
for el in range(1, n+1):
factorial *= el
print("Factorial of number", n, "is:", factorial)