0% found this document useful (0 votes)
5 views8 pages

ECE054 Lab4

The document contains a series of Python programming exercises focused on loops, conditionals, and basic algorithms. It includes code snippets for predicting outputs, identifying errors, and implementing various functions such as printing numbers, checking for prime numbers, and calculating factorials. Additionally, it presents problem-solving scenarios involving calculations based on user input.

Uploaded by

cocifis518
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

ECE054 Lab4

The document contains a series of Python programming exercises focused on loops, conditionals, and basic algorithms. It includes code snippets for predicting outputs, identifying errors, and implementing various functions such as printing numbers, checking for prime numbers, and calculating factorials. Additionally, it presents problem-solving scenarios involving calculations based on user input.

Uploaded by

cocifis518
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

09/12/2023, 21:35 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

It only shows upto 9 as 10 is not included in the code

#(b)
i=1
while i<=10:
print(i)
i=i+1

Correct Code

#(c)
i=3
while i<=10:
print(i)
i=i+2

"i" is starting from 3. So 1 and 2 is not possible

#(d)
i=1
while i<=10:
print(i)
i=i+1

Last statement do not have an indentation.

#(e)
i=1
while i>=10:
print(i)
i=i+1

Here "i" is greater than or equal to 10. Loop is false.

#(f)
i=1
while i>=10:
print(i)

Statement to increase the "i" not given.

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

The Break Statement

i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1

1
2
3

The Continue Statement

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

Enter The First Number: 5


Enter The Second Number: 10
6
7
8
9

#(b)
a,b=map(int,input("Enter Two Numbers: ").split())
a=a+1
while a<b:
print(a)
a=a+1

Enter Two Numbers: 3 9


4
5
6
7
8

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.

a=int(input("Enter The First Number:"))


b=int(input("Enter The Second Number:"))
for i in range(a,b):
if i%5==0 and i%7==0:
print(i)

Enter The First Number:1


Enter The Second Number:1000
35
70
105
140
175
210
245
280
315
350
385
420
455
490
525
560
595
630
665
700
735
770
805
840
875
910
945
980

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.

a=int(input("Enter The Number: "))


for i in range(1,a+1):
print((-1)**i*i)

Enter The Number: 6


-1
2
-3
4
-5
6

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.

n=int(input("Enter The Digit: "))


while n>0:
a=n%10
print(a)
n=n//10

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

Enter The Digit: 123


3
2
1

10) Write a python program to check if a number given by the user is a palindrome.

n=int(input("Enter The Number: "))


temp=n
rev=0
while(n>0):
digit=n%10
rev=rev*10+digit
n=n//10
if(temp==rev):
print("The Number Is A Palindrome")
else:
print("The Number Is Not A Palindrome")

Enter The Number: 887


The Number Is Not A Palindrome

11) Write a Python program to print all divisors of a given number n.

a=int(input("Enter The Number: "))


for i in range(1,a+1):
if (a%i==0):
print(i)

Enter The Number: 10


1
2
5
10

12) Write a python program to print factorial of all numbers between a and b. Input a and b.

a=int(input("Enter The First Number: "))


b=int(input("Enter The Second Number: "))
k=1
for i in range(a,b+1):
k*=i
print("The Factorials Are:",k)

Enter The First Number: 1


Enter The Second Number: 4
The Factorials Are: 1
The Factorials Are: 2
The Factorials Are: 6
The Factorials Are: 24

13) Write a Python program to check whether a given number is prime or not.

a=int(input("Enter The Number: "))


if a/1==a and a/a==1:
print(a,"Is A Prime Number")
else:
print(a,"Is Not A Prime Number")

Enter The Number: 13


13 Is A Prime Number

14) Write a Python program to compute the GCD (also called HCF) of two given numbers.

x=int(input("Enter The First Number: "))


y=int(input("Enter The Second Number: "))
if x>y:
small=y
else:
small=x
for i in range(1,small+1):
if x%i==0 and y%i==0:
hcf=i
print(hcf)

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

Enter The First Number: 4


Enter The Second Number: 8
4

15) Write a Python program to find the sum of the below series provided n is a number given by the user.

n=int(input("Enter the number of terms: "))


fact=1
for i in range(1,n+1):
fact=fact+(1/fact*i)
print("The sum of series is",round(fact))

Enter the number of terms: 4


The sum of series is 5

X= int(input("Enter value of x:"))


n=int(input("Enter value of n:"))
sum=x
sign=+1
for i in range(2,n+1):
fact=1
for j in range(1,i+1):
fact=fact*j
term=((x**i)*sign)/fact
sum=sum+term
sign=sign*-1
print(sum)
print("Sum of first",n, "terms:",sum)

Enter value of x:1


Enter value of n:3
20.0
Sum of first 3 terms: 20.0
12.0
Sum of first 3 terms: 12.0
76.0
Sum of first 3 terms: 76.0
44.0
Sum of first 3 terms: 44.0
54.666666666666664
Sum of first 3 terms: 54.666666666666664

keyboard_arrow_down Problem Solving


1. Vaidehi has two pet dogs, a Golden Retriever and a Great Dane. As per their nature, Golden Retriever is full of energy and wants to play
around while Great Dane is not a huge fan of exercise. Vaidehi knows that a Golden Retriever without exercise engages in destructive
behaviour, but she cannot leave Great Dane alone. So, she takes them for exercise every morning. While the Golden Retriever trots X meters
per minute, the Great Dane walks Y meters per minute, such that (Y < X). Given that the exercise time is N minutes, How many meters would
each dog cover? (Do not use the * operator)

x=int(input("Enter Distance By Gold: "))


y=int(input("Enter Distance By Dane: "))
t=int(input("Time Travelled: "))
print("Distance Covered By Golden Retriever: ",x*t)
print("Distance Covered By Great Dane: ",y*t)

Enter Distance By Gold: 5


Enter Distance By Dane: 3
Time Travelled: 10
Distance Covered By Golden Retriever: 50
Distance Covered By Great Dane: 30

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)

Enter Limak's Weight: 4


Enter Bob's Weight: 7
The Number Of Years Taken By Limak Will Be: 2

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.

• If he uses the machine on a copied document, he receives two additional copies.

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

You might also like