0% found this document useful (0 votes)
7 views5 pages

Manu py set 3

The document contains a series of programming exercises focused on generating random numbers, performing mathematical calculations, and manipulating user input in Python. Each exercise includes a description, sample code, and expected output. The tasks range from basic operations like generating random integers to more complex functions such as calculating leap years and drawing modular rectangles.

Uploaded by

ejoshua033
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
7 views5 pages

Manu py set 3

The document contains a series of programming exercises focused on generating random numbers, performing mathematical calculations, and manipulating user input in Python. Each exercise includes a description, sample code, and expected output. The tasks range from basic operations like generating random integers to more complex functions such as calculating leap years and drawing modular rectangles.

Uploaded by

ejoshua033
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

En.no.

: 22171341203 Batch: CE54


Name: Emmanuel Zurumba

Practical 3

1. Write a program that generates and prints 50 random integers, each between 3 and 6.

import random
random_numbers = [random.randint(3, 6) for _ in
range(50)] print(random_numbers)

O/P: [3, 6, 3, 4, 3, 4, 3, 5, 5, 5, 5, 5, 3, 6, 4, 5, 4, 5, 4, 3, 3, 3, 4, 3, 6, 5, 6, 5, 5, 5, 4, 6, 3, 6, 6, 5, 3, 4, 3, 3, 5, 5, 6, 3, 5, 4, 4, 3, 6, 5]

2. Write a program that generates a random number, x, between 1 and 50, a random number y between 2 and 5, and computes x y .
import random
x = random.randint(1,
50) y =
random.randint(2, 5)
result = x ** y
print(f"x*y = {result}")

O/P: x*y = 371293

3. Write a program that generates a random number between 1 and 10 and prints your name that many times.
import random
num = random.randint(1, 10)
print(num)
for i in range(num):
print("Universe")

O/P: 2
Universe
Universe

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.

import random
random_decimal = round(random.uniform(1, 10), 2)
print(random_decimal)

O/P: 5.56

5. Write a program that generates 50 random numbers such that the first number is between 1 and 2, the second is between 1 and 3, the
third is between 1 and 4, . . . , and the last is between 1 and 51.
import random

for i in range(1, 51):


print(random.randint(1, i+1))
O/P: 1
3
2
1
6
7

6. Write a program that asks the user to enter two numbers, x and y, and computes |x−y/x+y .

x = float(input("Enter the value of x: "))


y = float(input("Enter the value of y: "))
if x + y == 0:
print("Error: Division by zero is not
allowed.") else:
result = (x - y) / (x + y)
En.no.: 22171341203 Batch: CE54
Name: Emmanuel Zurumba

print(f"The result of (x - y) / (x + y) is: {result}")


En.no.: 22171341203 Batch: CE54
Name: Emmanuel Zurumba

O/P: Enter the value of x: 10


Enter the value of y: 5
The result of (x - y) / (x + y) is: 0.3333333333333333

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◦

angle = int(input("Enter an angle between -180 and 180 degrees: "))


while angle < -180 or angle > 180:
print("Invalid angle. Please enter an angle between -180 and 180 degrees.")
angle = int(input("Enter an angle between -180 and 180 degrees: "))
converted_angle = (angle + 360) % 360
print(f"The equivalent angle between 0 and 360 degrees is: {converted_angle} degrees")

O/P: Enter an angle between -180 and 180 degrees: 120


The equivalent angle between 0 and 360 degrees is: 120 degrees

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.]

seconds = int(input("Enter a number of seconds: "))


minutes = seconds // 60
remaining_seconds = seconds % 60
print(f"{minutes} minutes and {remaining_seconds} seconds")

O/P: Enter a number of seconds: 130


2 minutes and 10 seconds

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 future. An example is shown below. Enter hour: 8 How many hours ahead? 5 New
hour: 1 o'clock

hour = int(input("Enter hour (1-12): "))


hours_ahead = int(input("How many hours ahead? "))
new_hour = (hour + hours_ahead - 1) % 12 + 1
print(f"New hour: {new_hour} o'clock")

O/P: Enter hour (1-12): 6


How many hours ahead? 12
New hour: 6 o'clock

10. 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.

power = int(input("Enter a power: "))


last_digit = (2 ** power) % 10
print(f"The last digit of 2 raised to the power of {power} is {last_digit}.")

O/P: Enter a power: 4


The last digit of 2 raised to the power of 4 is 6.

11. Write a program that asks the user to enter a weight in kilograms. The program should convert it to pounds, printing the
answer rounded to the nearest tenth of a pound

weight_kg = float(input("Enter a weight in kilograms:


")) weight_lb = weight_kg * 2.20462
print(f"The weight in pounds is {weight_lb:.1f} pounds.")

O/P: Enter a weight in kilograms: 70


The weight in pounds is 154.3 pounds.

12. Write a program that asks the user for a number and prints out the factorial of that number

num = int(input("Enter a number: "))


result = 1
for i in range(1, num + 1):
result *= i
print(f"The factorial of {num} is {result}.")
En.no.: 22171341203 Batch: CE54
Name: Emmanuel Zurumba

O/P: Enter a number: 6


The factorial of 6 is 720.

13. Write a program that asks the user for a number and then prints out the sine, cosine, and tangent of that number

import math
num = float(input("Enter a number in radians: "))
sin_result = math.sin(num)
cos_result = math.cos(num)
tan_result = math.tan(num)
print(f"The sine of {num} is {sin_result}.")
print(f"The cosine of {num} is {cos_result}.")
print(f"The tangent of {num} is
{tan_result}.")

O/P: Enter a number in radians: 45


The sine of 45.0 is 0.8509035245341184.
The cosine of 45.0 is 0.5253219888177297.
The tangent of 45.0 is 1.6197751905438615.

14. Write a program that asks the user to enter an angle in degrees and prints out the sine of that angle.

import math
angle_degrees = float(input("Enter an angle in degrees: "))
angle_radians = math.radians(angle_degrees)
sin_result = math.sin(angle_radians)
print(f"The sine of {angle_degrees} degrees is {sin_result}.")

O/P: Enter an angle in degrees: 180


The sine of 180.0 degrees is 1.2246467991473532e-16.

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: 0 --- 0.0 1.0 15 --- 0.2588 0.9659 30 --- 0.5 0.866 ... 345----0.2588 0.9659

import math
for angle_degrees in range(0, 346, 15):
angle_radians = math.radians(angle_degrees)
sin_result = round(math.sin(angle_radians), 4)
cos_result = round(math.cos(angle_radians), 4)
print(f"{angle_degrees} {sin_result} {cos_result}")

O/P: 0----0.0 1.0


15 --- 0.2588 0.9659
30 --- 0.5 0.866
45 --- 0.7071 0.7071
60 --- 0.866 0.5
75 --- 0.9659 0.2588
90 --- 1.0 0.0
105 --- 0.9659 -0.2588
120 --- 0.866 -0.5
135 --- 0.7071 -0.7071
150 --- 0.5 -0.866
165 --- 0.2588 -0.9659
180 --- 0.0 -1.0
195 --- -0.2588 -0.9659
210 --- -0.5 -0.866
225 --- -0.7071 -0.7071
240 --- -0.866 -0.5
255 --- -0.9659 -0.2588
270 --- -1.0 -0.0
285 --- -0.9659 0.2588
300 --- -0.866 0.5
315 --- -0.7071 0.7071
330 --- -0.5 0.866
345 --- -0.2588 0.9659

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. def count_leap_years(year):
count = 0
En.no.: 22171341203 Batch: CE54
Name: Emmanuel Zurumba

for y in range(1600, year):


if (y % 4 == 0 and y % 100 != 0) or (y % 400 == 0):
count += 1
return count
year = int(input("Enter a year: "))
print("There have been", count_leap_years(year), "leap years between 1600 and", year)

O/P: Enter a year: 2024


There have been 103 leap years between 1600 and 2024

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.]
def calculate_change(amount):

amount = int(amount * 100)


quarters = amount // 25
amount %= 25
dimes = amount // 10
amount %= 10
nickels = amount // 5
amount %= 5
pennies = amount
return quarters, dimes, nickels, pennies
amount = float(input("Enter amount of change (less than $1.00):
")) if amount < 0 or amount >= 1:
print("Invalid amount. Please enter an amount less than
$1.00.") else:
quarters, dimes, nickels, pennies =
calculate_change(amount) print(f"To make ${amount:.2f} in
change, you will need:")
print(f" * {quarters} quarters")
print(f" * {dimes} dimes")
print(f" * {nickels} nickels")
print(f" * {pennies} pennies")

O/P: To make $0.46 in change, you will need:


* 1 quarters
* 2 dimes
* 0 nickels
* 1 pennies

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 × 5 rectangle and a 4 × 8.

def draw_modular_rectangle(width, height):


for i in range(height):
for j in range(width):
print((i * width + j) % 10, end='
') print()
width = int(input("Enter the width of the rectangle: "))
height = int(input("Enter the height of the rectangle:
")) draw_modular_rectangle(width, height)

O/P: Enter the width of the rectangle:


10 Enter the height of the rectangle: 4
0123456789
0123456789
0123456789
0123456789

You might also like