ECE054 Lab4
ECE054 Lab4
ipynb - Colaboratory
1) Predict the output of the below code snippet. Run it and see what you get.
i=1
while (i<10):
print("Amrita")
i=i+1
Amrita
Amrita
Amrita
Amrita
Amrita
Amrita
Amrita
Amrita
Amrita
2) Which out of the code snippets below, print the numbers from 1 to 10. Give the reason for the error in the code snippets below which does
not print from 1 to 10.
#(a)
i=1
while i<10:
print(i)
i=i+1
#(b)
i=1
while i<=10:
print(i)
i=i+1
Correct Code
#(c)
i=3
while i<=10:
print(i)
i=i+2
#(d)
i=1
while i<=10:
print(i)
i=i+1
#(e)
i=1
while i>=10:
print(i)
i=i+1
#(f)
i=1
while i>=10:
print(i)
https://colab.research.google.com/drive/1NhsjdRFIoBW4bSPJiYc2D1ob0zEVc4f4?usp=drive_open#scrollTo=kzwvxnFrlIzf&printMode=true 1/8
09/12/2023, 21:35 ECE054-Lab4.ipynb - Colaboratory
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
1
2
3
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
1
2
4
5
6
"for" Loop
#(a)
for x in "python":
print(x)
p
y
t
h
o
n
#(b)
for x in "python":
print(x,end=' ')
p y t h o n
#(c)
languages = ['Swift', 'Python', 'Go']
for language in languages:
print('Hello')
print('Hi')
Hello
Hi
Hello
Hi
Hello
Hi
#(d)
# use of range() to define a range of values
values = range(4)
# iterate from i = 0 to i = 3
for i in values:
print(i)
0
1
2
3
https://colab.research.google.com/drive/1NhsjdRFIoBW4bSPJiYc2D1ob0zEVc4f4?usp=drive_open#scrollTo=kzwvxnFrlIzf&printMode=true 2/8
09/12/2023, 21:35 ECE054-Lab4.ipynb - Colaboratory
#(e)
for i in range(4):
print(i)
0
1
2
3
#(f)
for i in range(2,10):
print(i)
2
3
4
5
6
7
8
9
#(g)
for i in range(1,30,2):
print(i)
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
3) Write a python program to take two integers from the user as input and print all integers between them using (a) for loop (b) while loop.
#(a)
a=int(input("Enter The First Number: "))
b=int(input("Enter The Second Number: "))
for i in range(a+1,b):
print(i)
i=i+1
#(b)
a,b=map(int,input("Enter Two Numbers: ").split())
a=a+1
while a<b:
print(a)
a=a+1
4) Write a Python program to print those numbers which are divisible by 7, between 150 and 270 (both included).
https://colab.research.google.com/drive/1NhsjdRFIoBW4bSPJiYc2D1ob0zEVc4f4?usp=drive_open#scrollTo=kzwvxnFrlIzf&printMode=true 3/8
09/12/2023, 21:35 ECE054-Lab4.ipynb - Colaboratory
a=150
b=270
for i in range(a,b+1):
if i%7==0:
print(i)
154
161
168
175
182
189
196
203
210
217
224
231
238
245
252
259
266
5) Write a Python program to print those numbers which are divisible by 7,and by 5, between a and b, where a and b are two numbers given by
the user.
6) Write a Python program that prints all the numbers from 0 to 6 except 3 and 6
for i in range(6):
if (i == 3 or i==6):
continue
print(i)
0
1
2
4
5
7) Write a Python program that prints all the numbers from 0 to 100 except multiples of 3 or 5.
https://colab.research.google.com/drive/1NhsjdRFIoBW4bSPJiYc2D1ob0zEVc4f4?usp=drive_open#scrollTo=kzwvxnFrlIzf&printMode=true 4/8
09/12/2023, 21:35 ECE054-Lab4.ipynb - Colaboratory
for i in range(101):
if(i%3==0 or i%5==0):
continue
print(i)
1
2
4
7
8
11
13
14
16
17
19
22
23
26
28
29
31
32
34
37
38
41
43
44
46
47
49
52
53
56
58
59
61
62
64
67
68
71
73
74
76
77
79
82
83
86
88
89
91
92
94
97
98
8) Write a Python program to print the first k terms in the following sequence. −1, 2, −3, 4, . . . (−1)^k ∗ k.
9) Write a Python program to take an n-digit integer and print the digits of the number from left to right and right to left.
https://colab.research.google.com/drive/1NhsjdRFIoBW4bSPJiYc2D1ob0zEVc4f4?usp=drive_open#scrollTo=kzwvxnFrlIzf&printMode=true 5/8
09/12/2023, 21:35 ECE054-Lab4.ipynb - Colaboratory
10) Write a python program to check if a number given by the user is a palindrome.
12) Write a python program to print factorial of all numbers between a and b. Input a and b.
13) Write a Python program to check whether a given number is prime or not.
14) Write a Python program to compute the GCD (also called HCF) of two given numbers.
https://colab.research.google.com/drive/1NhsjdRFIoBW4bSPJiYc2D1ob0zEVc4f4?usp=drive_open#scrollTo=kzwvxnFrlIzf&printMode=true 6/8
09/12/2023, 21:35 ECE054-Lab4.ipynb - Colaboratory
15) Write a Python program to find the sum of the below series provided n is a number given by the user.
2. Bear Limak wants to become the largest of bears, or at least to become larger than his brother Bob. Right now, Limak and Bob weigh a and
b respectively. It's guaranteed that Limak's weight is smaller than or equal to his brother's weight. Limak eats a lot and his weight is tripled
after every year, while Bob's weight is doubled after every year. After how many full years will Limak become strictly larger (strictly heavier)
than Bob?
https://colab.research.google.com/drive/1NhsjdRFIoBW4bSPJiYc2D1ob0zEVc4f4?usp=drive_open#scrollTo=kzwvxnFrlIzf&printMode=true 7/8
09/12/2023, 21:35 ECE054-Lab4.ipynb - Colaboratory
x=int(input("Enter Limak's Weight: "))
y=int(input("Enter Bob's Weight: "))
i=0
while x<y:
x*=3
y*=2
i+=1
print("The Number Of Years Taken By Limak Will Be: ",i)
3. Utkarsh, an inventive boy from Telangana, has created an unusual copying machine. Here's how it works:
• If he feeds the machine an original document, he gets back one more original document and one copy.
Initially, Utkarsh has only one original document. He wants to determine if it's possible to use the machine to obtain exactly 'x' copied
documents and 'y' original documents. Importantly, he cannot discard any documents, and he can only apply the machine to a copied
document if he currently has at least one copy. Your task is to write a program that takes 'x' and 'y' as inputs and prints "YES" if it's possible to
achieve these quantities using the machine, or "NO" if it's not possible.
x=int(input("Enter x: "))
y=int(input("Enter y: "))
while y>0:
if x==0:
print("NO")
break
elif y==1 and x%2==0:
print("NO")
break
x,y=y-1,x-1
else:
print("YES")
break
Enter x: 6
Enter y: 3
YES
https://colab.research.google.com/drive/1NhsjdRFIoBW4bSPJiYc2D1ob0zEVc4f4?usp=drive_open#scrollTo=kzwvxnFrlIzf&printMode=true 8/8