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

CS1101 Assignment 06 Programming Tasks

Uploaded by

Prabhat Gupta
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 views2 pages

CS1101 Assignment 06 Programming Tasks

Uploaded by

Prabhat Gupta
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

CS1101 Assignment06_GroupB_2024

Login to the system. Create a directory called assignment06 under your home directory. Change
to that directory (using the cd command) and do all your work there. At the end of the class,
archive the directory assignment06 and upload to WeLearn.
1. (a) Define the function product_of_odds (n), which will calculate the product of all odd
numbers from 1 to n.
(b) Use a loop to test the function for values n=1 to n=9 and print the results.

2. (a) Define a function fibonacci that calculates the n-th Fibonacci number. You may recall
that Fibonacci sequence is defined as:

F(0)=0; F(1)=1; F(n)=F(n−1)+F(n−2) for n>1

(b) Show the usage of the function by calculating Fibonacci numbers from n=1 to n=8.

3. If you need to evaluate mathematical operations (log, sin, cos, pi etc), you need to
declare import math at the beginning of your program. For example, to find the value of
cosine of 60◦, your program should look like:
Import math
print [Link](60*[Link]/180)
##pi gives you the value of π and θπ/180 converts θ from degree to radian.
(a) Define a function tan_square that takes an argument t (in degrees) and returns the
square of the tangent of t. Remember to convert t from degrees to radians when using
trigonometric functions.
(b) Then, write a loop that uses this function to print θ and tan 2(θ) for
θ=0,15,30,45,60,75,90
4. Suppose the length of three sticks are given by three numbers. Define a function which
takes the three lengths (as arguments) and determines whether these sticks can form a
triangle. The function should return True or False depending on whether the triangle is
formed or not. Your program should take three numbers from the user and should print
whether a triangle is possible or not. [Hint: three lengths form a triangle if the sum of any
two lengths is greater than the third.]

5. Define a function called find_min that takes three numbers as arguments and returns the
smallest of the three. Test the function with 3 different sets of numbers.

6. (a) In a program, define a function takes an input argument x and calculates the quotient q
= x//4 and r = x%4 and returns q and r.
(b)In the same program, take an input from the user, store it in a variable (say, s). Now,
call the function f(s) with two storage variables (to store the output). Finally, print the
stored variables.
Additional practice problems that will not be marked. All should attempt to solve these
problems in the lab itself after you finish problems 1-6.
7. Write a program which takes two integers from the user and returns all prime numbers
between the two integers. The result should not depend on the order in which the integers
are entered by the user.

8. Define a function grade_calculator that takes three arguments: assignment_score,


exam_score, and participation_score, each ranging from 0 to 100. The function should
calculate the final grade based on these weights:

 Assignments: 40% of the final grade


 Exam: 50% of the final grade
 Participation: 10% of the final grade

The function should then return the final grade as a percentage and also the corresponding
letter grade based on this scale:

The function should then return the final grade as a percentage and also the corresponding
letter grade based on this scale:

 90–100: A
 80–89: B
 70–79: C
 60–69: D
 Below 60: F

Write a program that uses grade_calculator to print the final grade and letter grade for three
different sets of scores.

Common questions

Powered by AI

To calculate a final grade from assignment, exam, and participation scores, use specific weightings such as 40% for assignments, 50% for exams, and 10% for participation. The final grade is the weighted sum of these scores. You can then map this percentage to a letter grade using predefined score ranges. Here’s an example in Python: ``` def grade_calculator(assignment_score, exam_score, participation_score): final_score = (assignment_score * 0.4) + (exam_score * 0.5) + (participation_score * 0.1) if final_score >= 90: return final_score, 'A' elif final_score >= 80: return final_score, 'B' elif final_score >= 70: return final_score, 'C' elif final_score >= 60: return final_score, 'D' else: return final_score, 'F' ``` This function returns both the numerical final score and its corresponding letter grade .

The algorithm to determine if a number is prime involves checking if it has any divisors other than 1 and itself, up to its square root. To print all prime numbers between two integers, you can loop through the range and apply the primality test to each number. Here's a basic implementation: ``` def is_prime(num): if num < 2: return False for i in range(2, int(num ** 0.5) + 1): if num % i == 0: return False return True start, end = 10, 50 # example values for num in range(min(start, end), max(start, end) + 1): if is_prime(num): print(num) ``` The function is_prime checks a number for primality, and the loop prints primes in the specified range regardless of order .

To create a Python function that determines if three stick lengths can form a triangle, you can use the triangle inequality theorem. This theorem states that for any three lengths to form a triangle, the sum of any two sides must be greater than the third side. Here’s an example function: ``` def can_form_triangle(a, b, c): return (a + b > c) and (a + c > b) and (b + c > a) ``` The function takes three arguments representing the lengths of the sticks and returns True if the conditions of the triangle inequality are met; otherwise, it returns False.

To implement a function that calculates the n-th Fibonacci number, you can use a recursive or iterative approach. For demonstration purposes, here's how you can define it using iteration: ``` def fibonacci(n): a, b = 0, 1 for _ in range(n): a, b = b, a + b return a for i in range(1, 9): print(f"Fibonacci({i}) = {fibonacci(i)}") ``` This function will correctly calculate and print Fibonacci numbers from n=1 to n=8 .

To define a function in Python that calculates the product of all odd numbers from 1 to n, you can write a function named 'product_of_odds' that iterates through numbers from 1 to n, checks if a number is odd, and multiplies it to a running product. This can be achieved using a loop. For example: ``` def product_of_odds(n): product = 1 for i in range(1, n+1): if i % 2 != 0: product *= i return product ```

To create a function that calculates the square of the tangent of an angle in degrees, you should first convert the angle from degrees to radians, since trigonometric functions in Python typically use radians. You can then use the math library to calculate the tangent and its square, like this: ``` import math def tan_square(t): radians = t * math.pi / 180 tan_value = math.tan(radians) return tan_value ** 2 ```

You might also like