Page 317 - CTS - CSA TP - Volume 2
P. 317
COMPUTER SOFTWARE APPLICATION - CITS
print(f”The sin of {angle} degrees is {sin_value}”)
print(f”The cos of {angle} degrees is {cos_value}”)
Explanation:
1 Importing the math Module:
• import math: This line imports the math module, which provides mathematical functions and constants.
2 Getting User Input:
• angle = float(input(“Enter an angle in degrees: “)): This line prompts the user to enter an angle in degrees,
captures the input as a string, and converts it to a floating-point number using float().
3 Calculating Sine and Cosine:
• sin_value = math.sin(math.radians(angle)): This line calculates the sine of the angle. The math.radians()
function converts the angle from degrees to radians before applying the math.sin() function.
• cos_value = math.cos(math.radians(angle)): This line calculates the cosine of the angle in a similar way.
4 Displaying the Results:
• print(f”The sin of {angle} degrees is {sin_value}”): This line prints the calculated sine value for the entered
angle.
• print(f”The cos of {angle} degrees is {cos_value}”): This line prints the calculated cosine value for the entered
angle.
Output:
TASK 3: Generating Random Numbers with the random Module
Code:
import random
random_number = random.randint(1, 100) # Generate a random integer between 1 and 100
print(f”Random number: {random_number}”)
Explanation:
1 Importing the random Module:
• import random: This line imports the random module, which provides functions for generating pseudo-random
numbers.
2 Generating a Random Integer:
• random_number = random.randint(1, 100): This line generates a random integer between 1 and 100 (inclusive)
using the randint() function from the random module. The result is stored in the variable random_number.
3 Displaying the Result:
• print(f”Random number: {random_number}”): This line prints the generated random number to the console
using an f-string.
302
CITS : IT & ITES - Computer Software Application - Exercise 137 CITS : IT & ITES - Computer Software Application - Exercise 137