-
Notifications
You must be signed in to change notification settings - Fork 0
/
while loop.py
124 lines (96 loc) · 1.31 KB
/
while 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
### Basic while loop
x=6
while x:
x-=1
print(x)
L=['red','green','blue']
while L:
#L.pop() print(L)
print(L.pop())
x='payel'
while x:
#x=x[0:] print(x)
print(x)
x = x[1:]
#### Break in While loop
x=6
while x:
print(x)
x-=1
if x==2:
break
### continue in while loop
x=10
while x:
x-=1
if x%2!=0:
continue
print(x)
### Else while loop
x=10
while x:
print(x)
x-=2
else:
print('Done!')
x=0
while x:
print(x)
x-=2
else:
print('Done!')
x=6
while x:
print(x)
x-=1
if x==2:
break
else:
print('Done!')
### While True game
#while True:
# name=input('Enter your name:')
# if name=='stop':
# break
# print('Hello,',name)
### While loop seris
#(1+2+3+4+...+100=?)
x=0
while x:
x=x+1
print(x)
#1+2+3+....+100
num=1
sum=0
while (num<=100):
sum=num+sum
num=num+1
print(sum)
n=100
s=int(n*(n+1)/2)
print(s,type(s))
### 1^2+2^2+3^2+....
n=1
s=0
while (n<=100):
s=(n**2)+s
n=n+1
print(s)
### 1^3+2^3+3^3+....+100^3
n=1
s=0
while (n<=100):
s=(n**3)+s
n=n+1
print(s)
#sum of natural number
while True:
n=int(input("Enter number:"))
s=0
if n<0:
print("negative or zero")
else:
while n>0:
s=n+s
n=n-1
print(s)