Rashmin patel 22171371089
PRACTICAL – 4
# 4.1 Write a program that generates and prints 50 random integers, each between 3 and 6.
print('-------practical 4.1-------')
from random import randint
for i in range(50):
x = randint(3,6)
print("A random number between 3 and 6:", x)
# 4.2 Write a program that generates a random number, x, between 1 and 50, a random number
y between 2 and 5, and computes xy
print('-----------------practical 4.2-----------------')
from random import randint
x = randint(1,50)
print('A random number between 1 and 50: ', x)
y = randint(2,5)
print('A random number between 2 and 5: ', y)
print(x^y)
# 4.3 Write a program that generates a random number between 1 and 10 and prints your
namethat many times.
print('-----------------practical 4.3-----------------')
from random import randint
a = randint(1,10)
for i in range(a):
print(a,"Rashmin")
# 4.4 Write a program that generates a random decimal number between 1 and 10 with two
decimal places of accuracy. Examples are 1.23, 3.45, 9.80, and 5.00.
Rashmin patel 22171371089
print('-------------------practical 4.4---------------')
import random
print(round([Link](1,10),2))
# 4.5 Write a program that generates 50 random numbers such that the first number is between
1and 2, the second is between 1 and 3, the third is between 1 and 4, . . . , and the last is between 1
and 51.
print('-------------------practical 4.5---------------')
from random import randint
for i in range(1,51):
x = randint(1,i)
print(x)
# 4.6 Write a program that asks the user to enter two numbers, x and y, and computes |x−y|
print('-------------------practical 4.6---------------')
x=int(input("enter the value of x:"))
y=int(input("enter the value of y:"))
ans1=((abs(x-y))/(x+y))
print(ans1)
# 4.7 Write a program that asks the user to enter an angle between −180◦ and 180◦ . Using an
expression with the modulo operator, convert the angle to its equivalent between 0◦ and 360
print('-------------------practical 4.7---------------')
from random import randint
X=randint(0,360)
angle=x
Rashmin patel 22171371089
a=360+angle
print(a)
# 4.8 Write a program that asks the user for a number of seconds and prints out how many
minutes and seconds that is. For instance, 200 seconds is 3 minutes and 20 seconds. [Hint: Use
the //operator to get minutes and the % operator to get seconds.]
print('-------------------practical 4.8---------------')
totalSecs = int(input("Enter seconds: "))
mins = totalSecs // 60
secs = totalSecs % 60
print(mins, "minutes and", secs, "seconds")
# 4.9 Write a program that asks the user for an hour between 1 and 12 and for how many hours in
the future they want to go. Print out what the hour will be that many hours into the [Link]
example is shown below.
print('-------------------practical 4.9---------------')
hours = int(input("Enter hour between 1-12 : "))
n = int(input("How many hours ahead : "))
s = hours + n
if s > 12:
s = 12
print("Time at that time would be : ", s, "O'clock")
# 4.10 (a) One way to find out the last digit of a number is to mod the number by 10. Write a
program that asks the user to enter a power. Then find the last digit of 2 raised to that power.
print('-------------------practical 4.10a---------------')
power = int(input("Enter a power: "))
last_digit = (2 ** power) % 10
print(f"The last digit of 2^{power} is: {last_digit}")
Rashmin patel 22171371089
#4.10 (b) One way to find out the last two digits of a number is to mod the number by 100. Write
a program that asks the user to enter a power. Then find the last two digits of 2 raised to that
power.
print('-------------------practical 4.10b---------------')
power = int(input("Enter a power: "))
last_digit = (2 ** power) % 100
print(f"The last digit of 2^{power} is: {last_digit}")
#4.10 (c) Write a program that asks the user to enter a power and how many digits they
[Link] the last that many digits of 2 raised to the power the user entered.
print('-------------------practical 4.10c---------------')
def last_digits_of_2_to_the_power(power, digits):
return (2 ** power) % (10 ** digits)
power = int(input("Enter a power: "))
digits = int(input("Enter the number of digits: "))
print("Last", digits, "digits:", last_digits_of_2_to_the_power(power, digits))
#4.11 Write a program that asks the user to enter a weight in kilograms. The program
shouldconvert it to pounds, printing the answer rounded to the nearest tenth of a pound.
print('-------------------practical 4.11---------------')
kilograms = float(input("Enter weight in kilograms: "))
pounds = kilograms * 2.20462
pounds_rounded = round(pounds, 1)
print(kilograms, "kilograms is equal to", pounds, "pounds.")
#4.12 Write a program that asks the user for a number and prints out the factorial of that number.
print('-------------------practical 4.12---------------')
Rashmin patel 22171371089
from math import factorial
number = int(input("Enter a number: "))
result = factorial(number)
print("The factorial of", number, "is", result)
#4.13 Write a program that asks the user for a number and then prints out the sine, cosine,
andtangent of that number.
print('-------------------practical 4.13---------------')
import math
degrees = float(input("Enter an angle in degrees: "))
radians = [Link](degrees)
sine = [Link](radians)
cosine = [Link](radians)
tangent = [Link](radians)
print(f"Sine of {degrees} degrees: {sine:.4f}")
print(f"Cosine of {degrees} degrees: {cosine:.4f}")
print(f"Tangent of {degrees} degrees: {tangent:.4f}")
#4.14 Write a program that asks the user to enter an angle in degrees and prints out the sine of
the tangle.
print('-------------------practical 4.14---------------')
import math
degrees = float(input("Enter an angle in degrees: "))
sine = [Link]([Link](degrees))
print(f"The sine of {degrees} degrees is: {sine}")
Rashmin patel 22171371089
#4.15 Write a program that prints out the sine and cosine of the angles ranging from 0 to 345◦in
15◦ increments. Each result should be rounded to 4 decimal places. Sample output is shown
below
print('-------------------practical 4.15---------------')
import math
for degrees in range(0, 346, 15):
sine = [Link]([Link](degrees))
cosine = [Link]([Link](degrees))
print(f"{degrees} --- {sine:.4f} {cosine:.4f}")
#4.16 Below is described how to find the date of Easter in any year. Despite its intimidating
appear-ance, this is not a hard problem. Note that bxc is the floor function, which for positive
numbers just drops the decimal part of the number. For instance b3.14c = 3. The floor function is
part of the math module.
import math
def easter_date(year):
# Step 1: Calculate intermediate values
a = year % 19
b = year // 100
c = year % 100
d = b // 4
e=b%4
f = (b + 8) // 25
g = (b - f + 1) // 3
h = (19 * a + b - d - g + 15) % 30
i = c // 4
k=c%4
l = (32 + 2 * e + 2 * i - h - k) % 7
Rashmin patel 22171371089
m = (a + 11 * h + 22 * l) // 451
# Step 2: Calculate the month and day of Easter
month = (h + l - 7 * m + 114) // 31
day = ((h + l - 7 * m + 114) % 31) + 1
return (month, day)
# Example usage
year = 2024
easter = easter_date(year)
print(f"Easter in {year} falls on: {easter[0]}/{easter[1]}")
#4.17 A year is a leap year if it is divisible by 4, except that years divisible by 100 are not leap
years unless they are also divisible by 400. Ask the user to enter a year, and, using the //
operator, determine how many leap years there have been between 1600 and that year.
print('-------------------practical 4.17---------------')
year = int(input("Enter a year: "))
leap_year_count = 0
for y in range(1600, year + 1):
if (y % 4 == 0 and y % 100 != 0) or (y % 400 == 0):
leap_year_count += 1
print(f"There have been {leap_year_count} leap years between 1600 and {year}.")
Rashmin patel 22171371089
#4.18 Write a program that given an amount of change less than $1.00 will print out exactly how
many quarters, dimes, nickels, and pennies will be needed to efficiently make that change.[Hint:
the // operator may be useful.]
print('-------------------practical 4.18---------------')
amount = float(input("Enter an amount of change less than $1.00: "))
cents = int(amount * 100)
quarters = cents
cents %= 25
dimes = cents
cents %= 10
nickels = cents
cents %= 5
pennies = cents
print(f"Change for ${amount:.2f} is:")
print(f"Quarters: {quarters}")
print(f"Dimes: {dimes}")
print(f"Nickels: {nickels}")
print(f"Pennies: {pennies}")
#4.19 Write a program that draws “modular rectangles” like the ones below. The user specifies
the width and height of the rectangle, and the entries start at 0 and increase typewriter fashion
from left to right and top to bottom, but are all done mod 10. Below are examples of a 3 ×
5rectangle and a 4 × 8.
print('-------------------practical 4.19---------------')
width = int(input("Enter the width of the rectangle: "))
height = int(input("Enter the height of the rectangle: "))
current_number = 0
for row in range(height):
row_values = []
Rashmin patel 22171371089
for col in range(width):
row_values.append(current_number % 10)
current_number += 1
print(" ".join(map(str, row_values)))
OUTPUT
-------practical 4.1-------
A random number between 3 and 6: 6
A random number between 3 and 6: 3
A random number between 3 and 6: 6
A random number between 3 and 6: 4
A random number between 3 and 6: 3
A random number between 3 and 6: 5
A random number between 3 and 6: 5
A random number between 3 and 6: 5
A random number between 3 and 6: 3
A random number between 3 and 6: 5
A random number between 3 and 6: 4
A random number between 3 and 6: 5
A random number between 3 and 6: 4
A random number between 3 and 6: 6
A random number between 3 and 6: 3
A random number between 3 and 6: 6
A random number between 3 and 6: 5
A random number between 3 and 6: 6
Rashmin patel 22171371089
A random number between 3 and 6: 6
A random number between 3 and 6: 3
A random number between 3 and 6: 5
A random number between 3 and 6: 5
A random number between 3 and 6: 5
A random number between 3 and 6: 6
A random number between 3 and 6: 4
A random number between 3 and 6: 3
A random number between 3 and 6: 5
A random number between 3 and 6: 5
A random number between 3 and 6: 6
A random number between 3 and 6: 6
A random number between 3 and 6: 5
A random number between 3 and 6: 3
A random number between 3 and 6: 4
A random number between 3 and 6: 6
A random number between 3 and 6: 3
A random number between 3 and 6: 4
A random number between 3 and 6: 4
A random number between 3 and 6: 6
A random number between 3 and 6: 6
A random number between 3 and 6: 3
A random number between 3 and 6: 5
A random number between 3 and 6: 4
A random number between 3 and 6: 5
A random number between 3 and 6: 4
A random number between 3 and 6: 4
Rashmin patel 22171371089
A random number between 3 and 6: 6
A random number between 3 and 6: 5
A random number between 3 and 6: 5
A random number between 3 and 6: 4
A random number between 3 and 6: 6
-----------------practical 4.2-----------------
A random number between 1 and 50: 19
A random number between 2 and 5: 3
16
-----------------practical 4.3-----------------
5 Rashmin
5 Rashmin
5 Rashmin
5 Rashmin
5 Rashmin
-------------------practical 4.4---------------
6.12
-------------------practical 4.5---------------
1
2
2
3
Rashmin patel 22171371089
12
10
10
20
14
17
1
8
14
12
30
12
5
Rashmin patel 22171371089
34
33
19
12
38
25
22
43
28
14
-------------------practical 4.6---------------
enter the value of x:5
enter the value of y:4
0.1111111111111111
-------------------practical 4.7---------------
365
-------------------practical 4.8---------------
Enter seconds: 7
0 minutes and 7 seconds
-------------------practical 4.9---------------
Enter hour between 1-12 : 5
How many hours ahead : 4
Time at that time would be : 9 O'clock
Rashmin patel 22171371089
-------------------practical 4.10a---------------
Enter a power: 5
The last digit of 2^5 is: 2
-------------------practical 4.10b---------------
Enter a power: 6
The last digit of 2^6 is: 64
-------------------practical 4.10c---------------
Enter a power: 3
Enter the number of digits: 5
Last 5 digits: 8
-------------------practical 4.11---------------
Enter weight in kilograms: 6
6.0 kilograms is equal to 13.227719999999998 pounds.
-------------------practical 4.12---------------
Enter a number: 44
The factorial of 44 is 2658271574788448768043625811014615890319638528000000000
-------------------practical 4.13---------------
Enter an angle in degrees: 54
Sine of 54.0 degrees: 0.8090
Cosine of 54.0 degrees: 0.5878
Tangent of 54.0 degrees: 1.3764
-------------------practical 4.14---------------
Enter an angle in degrees: 34
The sine of 34.0 degrees is: 0.5591929034707469
-------------------practical 4.15---------------
0 --- 0.0000 1.0000
15 --- 0.2588 0.9659
Rashmin patel 22171371089
30 --- 0.5000 0.8660
45 --- 0.7071 0.7071
60 --- 0.8660 0.5000
75 --- 0.9659 0.2588
90 --- 1.0000 0.0000
105 --- 0.9659 -0.2588
120 --- 0.8660 -0.5000
135 --- 0.7071 -0.7071
150 --- 0.5000 -0.8660
165 --- 0.2588 -0.9659
180 --- 0.0000 -1.0000
195 --- -0.2588 -0.9659
210 --- -0.5000 -0.8660
225 --- -0.7071 -0.7071
240 --- -0.8660 -0.5000
255 --- -0.9659 -0.2588
270 --- -1.0000 -0.0000
285 --- -0.9659 0.2588
300 --- -0.8660 0.5000
315 --- -0.7071 0.7071
330 --- -0.5000 0.8660
345 --- -0.2588 0.9659
Easter in 2024 falls on: 3/31
-------------------practical 4.17---------------
Enter a year: 2024
There have been 104 leap years between 1600 and 2024.
-------------------practical 4.18---------------
Rashmin patel 22171371089
Enter an amount of change less than $1.00: 4
Change for $4.00 is:
Quarters: 400
Dimes: 0
Nickels: 0
Pennies: 0
-------------------practical 4.19---------------
Enter the width of the rectangle: 33
Enter the height of the rectangle: 4
000000000000000000000000000000000
111111111111111111111111111111111
222222222222222222222222222222222
333333333333333333333333333333333