-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhile.py
More file actions
238 lines (194 loc) · 5 KB
/
while.py
File metadata and controls
238 lines (194 loc) · 5 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# Loops in Python: for loop, while loop
"""
for(i=1; i<=5; i++)
"""
# while loop
# Multiplicative table of a given integer
"""
n = int(input("n: ")) # 5
i = 1
while i <= 10:
print(f"{n} X {i} = {n*i}")
i += 1
Output:
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
"""
# Write a program in Python that takes an integer from user & prints the number of digits it has.
"""
a = int(input("Enter a number: "))
count = 0
if a == 0:
count = 1
else:
while a > 0:
a = a // 10
count += 1
print(count)
"""
"""
number = int(input("Enter a number: ")) # 5379
print(len(str(number))) # "5379"
"""
# using while loop to iterate through a collection:
"""
fruits = ["apple", "kiwi", "banana", "cherry", "grapes", "orange"]
i = 0
while i < len(fruits):
print(fruits[i])
i += 1
name = "James Bond"
i = 0
while i < len(name):
print(name[i])
i += 1
"""
# Biggest use of while loop in Python: To create infinite loop
"""
a = int(input("Enter two integers:\n"))
b = int(input())
while True:
operation = input("Enter operation (+, -, *, / or 'x' to quit): ").lower()
if operation == "+":
print(f"{a} + {b} = {a + b}")
elif operation == "-":
print(f"{a} - {b} = {a - b}")
elif operation == "*":
print(f"{a} * {b} = {a * b}")
elif operation == "/":
print(f"{a} / {b} = {a / b}")
# elif operation == "x" or operation == "X":
elif operation == "x":
break
else:
print("Invalid, please try again...")
"""
# False: False, 0, "", [], set(), (), {}
# True: True, 1 and everything other than above
# print(5 > 3)
# Write a Python program to take a integer from the user and print whether it is a Prime number or not.
"""
n = int(input("n: "))
i = 2
flag = 1
while i < n: # i=2,3,4,5,6,7,...,640
if n % i == 0:
print("Not Prime.")
flag = 0
break
i += 1
if flag == 1:
print("Prime.")
"""
# IMP Homework:
# Write a Python program that takes name of a fruit from user & prints whether it is in the following list or not:
# fruits = ["apple", "kiwi", "banana", "cherry", "grapes", "orange"]
# Additional programs:
"""
9. Write a Python program that prints all the Armstrong numbers between two integers given by user.
"""
"""
1. Write a Python code that takes an integer from user and prints number of digits in that integer.
num = input("Enter your number:-")
print("Digit :-",len(num))
"""
"""
2. Write down a Python code that creates a user defined list
l = []
while True:
num = input("Enter + for Add x for Exit:-").lower()
if num == "+":
n = input("Enter Any Element:")
l.append(n)
elif num == "x":
break
else:
print("Invalid Operation..")
if(len(l) == 0):
print("\nYour list is Empty...")
else:
print("\nYour List:",l)
"""
"""
3. Write a Python code to print each of the elements of a given list in a new line
l = ["hello","friends","chay","pilo","khana","khalo"]
i = 0
while i < len(l) :
print(l[i])
i += 1
"""
"""
4. Write a Python program that prints whether the number given is a prime number or not.
num = int(input("Enter your Number: "))
i = 2
count = 0
while i < num:
if num % i == 0:
print(f"{num} is not a Prime number...")
count = 1
break
i += 1
if count == 0:
print(f"{num} is a Prime Number....")
"""
"""
5. Write a Python program that prints whether the number given is a perfect number or not.
num = int(input("Enter your number: "))
i = 1
per = 0
while i < num:
if num % i == 0:
per = per + i
i += 1
if per == num:
print(num,"is a Perfect number")
else:
print(num,"is not a Perfrct number")
"""
"""
6. Write a Python program that prints whether the number given is an Armstrong number or not.
amg = input("Enter Your number:-")
i=0
sum=0
while(i<len(amg)):
sum += (int(amg[i]) ** len(amg))
i += 1
if sum == int(amg):
print("The Number is Armstrong")
else:
print("The Number isn't Armstrong")
"""
"""
7. Write a Python program that prints all the prime numbers between two integers given by user.
f = int(input("Enter your First Number: "))
s = int(input("Enter your Second Number: "))
print(f"\nPrime Number between {f} and {s}")
while f <= s:
i = 2
count = 0
while i < f:
if f % i == 0:
count = 1
break
i += 1
if count == 0:
print(f)
f += 1
"""
"""
8. Write a Python program that prints all the perfect numbers between two integers given by user.
f = int(input("Enter your First Number: "))
s = int(input("Enter your Second Number: "))
print(f"perfect numbers between {f} and {s}")
while f < s:
i = 2
per = 1
while i < f:
if f % i == 0:
per = per + i
i += 1
if per == f:
print(f)
f += 1
"""