100% found this document useful (1 vote)
20 views8 pages

Mit 214 - Introduction To Programming

Uploaded by

william joab
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
100% found this document useful (1 vote)
20 views8 pages

Mit 214 - Introduction To Programming

Uploaded by

william joab
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

MIT 214 - INTRODUCTION TO PROGRAMMING

February 28, 2024

1. Write a program that gets a random integer between 1000 and 20000, then checks if the
number is divisible by 2, 3, 5, 7, 11 or 13. Tells the user all the primes that divides the
number generated. If none divides the number print ‘None divides x’ where x represents the
number generated.

[16]: from random import randint


x = randint(1000,20000)
if x%2 == 0:
if x%3 == 0:
if x%5 == 0:
if x%7 == 0:
if x%11 == 0:
if x%13 == 0:
print(f'{x} is divisible by 2, 3, 5, 7, 11, and 13')
else:
print(f'{x} is divisible by 2, 3, 5, 7, and 11')
elif x%13 == 0:
print(f'{x} is divisible by 2, 3, 5, 7, and 13')
else:
print(f'{x} is divisible by 2, 3, 5,and 7')
elif x%11 == 0:
if x%13 == 0:
print(f'{x} is divisible by 2, 3, 5, 11, and 13')
else:
print(f'{x} is divisible by 2, 3, 11, and 5')
elif x%13:
print(f'{x} is divisible by 2, 3, 5, and 13')
else:
print(f'{x} is divisible by 2, 3, and 5')
elif x%11:
if x%13 == 0:
print(f'{x} is divisible by 2, 3, 11, and 13')
else:
print(f'{x} is divisible by 2, 3, and 11')
elif x%5 == 0:
if x%7 == 0:
if x%11 == 0:
if x%13 == 0:

1
print(f'{x} is divisible by 2, 5, 7, 11, and 13')
else:
print(f'{x} is divisible by 2, 5, 7, and 11')
elif x%13 == 0:
print(f'{x} is divisible by 2, 5, 7, and 13')
else:
print(f'{x} is divisible by 2, 5, and 7')
elif x%11 == 0:
if x%13 == 0:
print(f'{x} is divisible by 2, 5, 11, and 13')
else:
print(f'{x} is divisible by 2, 5, and 11')
elif x%13 == 0:
print(f'{x} is divisible by 2, 5, and 13')
else:
print(f'{x} is divisible by 2 and 5')
elif x%7 == 0:
if x%11 == 0:
if x%13 == 0:
print(f'{x} is divisible by 2, 7, 11, and 13')
else:
print(f'{x} is divisible by 2, 7, and 11')
elif x%13 == 0:
print(f'{x} is divisible by 2, 7, and 13')
else:
print(f'{x} is divisible by 2 and 7')
elif x%11 == 0:
if x%13 == 0:
print(f'{x} is divisible by 2, 11, and 13')
else:
print(f'{x} is divisible by 2, and 11')
elif x%13 == 0:
print(f'{x} is divisible by 2 and 13')
else:
print(f'{x} is divisible by 2')
elif x%3 == 0:
if x%5 == 0:
if x%7 == 0:
if x%11 == 0:
if x%13 == 0:
print(f'{x} is divisible by 3, 5, 7, 11, and 13')
else:
print(f'{x} is divisible by 3, 5, 7, and 11')
elif x%13 == 0:
print(f'{x} is divisible by 3, 5, 7, and 13')
else:
print(f'{x} is divisible by 3, 5,and 7')

2
elif x%11 == 0:
if x%13 == 0:
print(f'{x} is divisible by 3, 5, 11, and 13')
else:
print(f'{x} is divisible by 3, 11, and 5')
elif x%13:
print(f'{x} is divisible by 3, 5, and 13')
else:
print(f'{x} is divisible by 3, and 5')
elif x%7 == 0:
if x%11 == 0:
if x%13 == 0:
print(f'{x} is divisible by 3, 7, 11, and 13')
else:
print(f'{x} is divisible by 3, 5, 7, and 11')
elif x%13 == 0:
print(f'{x} is divisible by 3, 7, and 13')
else:
print(f'{x} is divisible by 3 and 7')
elif x%11:
if x%13 == 0:
print(f'{x} is divisible by 3, 11, and 13')
else:
print(f'{x} is divisible by 3, and 11')
elif x%13 == 0:
print(f'{x} is divisible by 3 and 13')
else:
print(f'{x} is divisible by 3')
elif x%5 == 0:
if x%7 == 0:
if x%11 == 0:
if x%13 == 0:
print(f'{x} is divisible by 5, 7, 11, and 13')
else:
print(f'{x} is divisible by 5, 7, and 11')
elif x%13 == 0:
print(f'{x} is divisible by 5, 7, and 13')
else:
print(f'{x} is divisible by 3, 5,and 7')
elif x%11 == 0:
if x%13 == 0:
print(f'{x} is divisible by 5, 11, and 13')
else:
print(f'{x} is divisible by 11, and 5')
elif x%13:
print(f'{x} is divisible by 3, 5, and 13')
else:

3
print(f'{x} is divisible by 5')
elif x%7 == 0:
if x%11 == 0:
if x%13 == 0:
print(f'{x} is divisible by 7, 11, and 13')
else:
print(f'{x} is divisible by 7 and 11')
elif x%13 == 0:
print(f'{x} is divisible by 7 and 13')
else:
print(f'{x} is divisible by 7')
elif x%11:
if x%13 == 0:
print(f'{x} is divisible by 11 and 13')
else:
print(f'{x} is divisible by 11')
elif x%13 == 0:
print(f'{x} is divisible by 13')
else:
print(f'{x} is divisible by None of the primes')

6160 is divisible by 2, 5, 7, and 11


2. Write a python program that generates an integer between 0 and 10, asks the user to guess
the number. If the user gets it right prints ‘You win at 0 moves’. Using nested if statements
instead of a loop let the user guess up to 5 times, if the user gets it right at i th guess inform
the user. After every guess advice the user to guess higher or lower. After the fifth guess
print ‘Oops..You lose!’.

[23]: from random import randint


x = randint(0,10)

guess = eval(input('Guess a number: '))


if guess == x:
print('You win at zero moves')
elif guess > x:
guess = eval(input('Guess lower: '))
if guess == x:
print('You win at 1 move')
elif guess > x:
guess = eval(input('Guess lower: '))
if guess == x:
print('You win at 2 move')
else:
if guess > x:
guess = eval(input('Guess lower: '))
if guess == x:
print('You win at 3 move')

4
else:
if guess > x:
guess = eval(input('Guess lower: '))
if guess == x:
print('You win at 4 moves')
else:
print('You lose')
else:
guess = eval(input('Guess Upper: '))
if guess == x:
print('You win at 4 moves')
else:
print('You lose')
else:
guess = eval(input('Guess upper: '))
if guess == x:
print('You win at 2 moves')
elif guess > x:
guess = eval(input('Guess lower: '))
if guess == x:
print('You win at 3 move')
else:
if guess > x:
guess = eval(input('Guess lower: '))
if guess == x:
print('You win at 4 moves')
else:
print('You lose')
else:
guess = eval(input('Guess Upper: '))
if guess == x:
print('You win at 4 moves')
else:
print('You lose')
else:
guess = eval(input('Guess upper: '))
if guess == x:
print('You win at 3 move')
elif guess > x:
guess = eval(input('Guess lower: '))
if guess == x:
print('You win at 4 moves')
else:
print('You lose')
else:
guess = eval(input('Guess Upper: '))
if guess == x:

5
print('You win at 4 moves')
else:
print('You lose')

else:
guess = eval(input('Guess upper: '))
if guess == x:
print('You win at 1 move')
elif guess > x:
guess = eval(input('Guess lower: '))
if guess == x:
print('You win at 2 move')
else:
if guess > x:
guess = eval(input('Guess lower: '))
if guess == x:
print('You win at 3 move')
else:
if guess > x:
guess = eval(input('Guess lower: '))
if guess == x:
print('You win at 4 moves')
else:
print('You lose')
else:
guess = eval(input('Guess Upper: '))
if guess == x:
print('You win at 4 moves')
else:
print('You lose')
else:
guess = eval(input('Guess upper: '))
if guess == x:
print('You win at 2 moves')
elif guess > x:
guess = eval(input('Guess lower: '))
if guess == x:
print('You win at 3 move')
else:
if guess > x:
guess = eval(input('Guess lower: '))
if guess == x:
print('You win at 4 moves')
else:
print('You lose')
else:
guess = eval(input('Guess Upper: '))

6
if guess == x:
print('You win at 4 moves')
else:
print('You lose')
else:
guess = eval(input('Guess upper: '))
if guess == x:
print('You win at 3 move')
elif guess > x:
guess = eval(input('Guess lower: '))
if guess == x:
print('You win at 4 moves')
else:
print('You lose')
else:
guess = eval(input('Guess Upper: '))
if guess == x:
print('You win at 4 moves')
else:
print('You lose')

Guess a number: 6
Guess upper: 7
Guess upper: 9
Guess lower: 8
You win at 3 move
3. Write a program to calculate the number of days, hours, minutes and seconds in 955300
seconds.

[25]: secs = 955300


days = secs//(60*60*24)
secs -= days*60*60*24
if days == 1:
msg = '1 day, '
else:
msg = str(days) + ' days, '
hrs = secs//(60*60)
secs -= hrs*60*60
if hrs == 1:
msg += '1 hour, '
else:
msg += str(hrs) + ' hours, '
mins = secs//60
secs -= mins*60
if mins == 1:
msg += '1 minute, and '
else:

7
msg += str(mins) + ' minutes, and '
if secs == 1:
msg += '1 second.'
else:
msg += str(secs) + ' seconds.'
print(msg)

11 days, 1 hour, 21 minutes, and 40 seconds.


4. Given the following: 𝑘 = 10, 𝑦 = 3, 𝑥 = 4, 𝑤 = 5, write a program that prints the result of

𝑗 = 𝑘 ÷ 𝑦2 × 4 ÷ 𝑦 + 𝑥2−𝑘

[29]: k, y, x, w = 10, 3, 4, 5
j = k/y**2*4/y+x**(2-k)
print(f"j = {j}")

j = 1.4814967402705441
5. Write a program that asks the user to input three real numbers, then returns a tuple with
the numbers arranged in an ascending order. A tuple is written as follows: (2, 3, 5).

[33]: first = eval(input("Enter the 1st number: "))


second = eval(input("Enter the 2nd number: "))
third = eval(input("Enter the 3rd number: "))

if first > second:


first, second = second, first
if second > third:
second, third = third, second
if first > second:
first, second = second, first
print((first, second, third))

Enter the 1st number: 1


Enter the 2nd number: 8
Enter the 3rd number: 4
(1, 4, 8)

You might also like