-
Notifications
You must be signed in to change notification settings - Fork 4
/
Loop.py
149 lines (118 loc) · 2.23 KB
/
Loop.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
for i in range(10):
print(i,end=' ')
print()
'''
Output:
0 1 2 3 4 5 6 7 8 9
'''
for i in range(10, 0, -1):
print(i, end=' ')
print()
'''
Output:
10 9 8 7 6 5 4 3 2 1
'''
for i in reversed(range(10)):
print(i, end=' ')
print()
'''
Output:
9 8 7 6 5 4 3 2 1 0
'''
i = 10
while i != 0:
print(i, end=' ')
i -= 1
print()
'''
Output:
10 9 8 7 6 5 4 3 2 1
'''
# 1 + 2 + 3 + ....... + n
### for loop
n = int(input())
sm = 0
for i in range(1, n + 1):
sm += i
print(sm)
'''
Input:
100
Output:
5050
'''
### while loop
n = int(input())
i = 0
sm = 0
while i <= n:
sm += i
i += 1
print(sm)
# Counts how many 2 divides 100
x = 100
cnt = 0
while x%2 == 0:
x = x//2
cnt += 1
print(cnt)
'''
Output:
2
'''
#### Finds out the highest number which is power of 2 and less than 1000
x = 1
# if multiplying by 2 does not exceed 1000, multiply
while x*2 < 1000:
x *= 2
print(x)
#### prints odd numbers 1 to 10
for i in range(1, 10 + 1):
if i % 2 == 0: continue
print(i, end= ' ')
print()
### Print only 1,2,3
for i in range(1, 11):
if i > 3:
break
print(i, end=' ')
print()
##### 1 + (1+2) + (1+2+3) + .... + (1+2+3+..+n)
n = int(input())
sm = 0
for i in range(1, n+1):
for j in range(1, i):
sm += j
print(sm)
### Simple function for problem solving
def Main(): # user define function
n = int(input())
# now we will calculate the factorial of a number
fact = 1
for i in range(1, n + 1):
fact *= i
print(fact)
if __name__ == '__main__': # python3 'main' function
Main() # call the user define function 'Main()'
###########
### EOF
while True:
try:
a = input()
print(a)
except EOFError:
break
'''
** You are now able to solve following online-judge problems.
------------------------------------------------------------
(1) URI Online Judge | 1103 Alarm Clock
(2) Timus | 1083. Factorials!!!
(3) Timus | 1209. 1, 10, 100, 1000...
(4) URI Online Judge | 1161 Factorial Sum
(5) URI Online Judge | 1059 Even Numbers
(6) HackerRank | Loops
(7) Spoj | TEST - Life, the Universe, and Everything
(8) CodeChef | Life, the Universe, and Everything
(9) CodeChef | N different palindromes
(10) UVa | 10055 Hashmat the Brave Warrior
'''