You are on page 1of 4

Assignment TWO

Course: CS 1101-01 Programming Fundamentals - AY2024-T2


BY: Kalid Hassen Yasin
Part 1
The circumference of a circle is calculated by 2πr, where π = 3.14159 (rounded to five decimal places).
Write a function calledprint_circumthat takes an argument for the circle's radius and prints the circle's
circumference.
Call yourprint_circumfunction three times with different values for radius.
Include the following in your part 1 submission:

The code for your print_circum function.

import math

radius = float(input("Enter the radius of the circle"))

print_circum = 2*math.pi*radius

print("circumference of circle is:", print_circum

# Example calls to the print_circum function

print_circum(6)

print_circum(8.5)

print_circum(9)

Here are the inputs and outputs for three calls of the print_circum function:
1. print_circum (6); The output: The circumference of a circle with radius 6 is
37.699079999999995
2. print_circum(8.5); The output: The circumference of a circle with radius 8.5 is 53.40703
3. print_circum(2.5); The output: The circumference of a circle with radius 9 is 56.54862
In the function print_circum, the initial step involves computing the circumference of a circle
using the formula 2πr, where the value of π is approximated as 3.14159. Subsequently, the result
is displayed using the console.log() method. Upon invoking the print_circum function with a
radius of 6, the output indicates a circumference of 37.699079999999995, which rounds to
37.69908 when truncated to five decimal places. Similarly, when the function is executed with a
radius of 8.5, the resulting circumference is 53.40703. Furthermore, when the function is called
with a radius of 9, the output specifies the circumference of a circle with a radius of 56.54862.
Part 2
Welcome to your first project. Develop a catalog for a company. Assume that this company sells three
different Items. The seller can sell individual items or a combination of any two items. A gift pack is a
special combination that contains all three items. Here are some special considerations:
A. If a customer purchases individual items, he does not receive any discount.
B. If a customer purchases a combo pack with two unique items, he gets a 10% discount.
C. If the customer purchases a gift pack, he gets a 25% discount.
Write a function for the above scenario. Perform the calculations in code wherever applicable. The
function should be your own creation, not copied from any other source. The final output should look
like:
Include the following in your part 2 submission:

• The code for the function that you created.


• The Output of the code.
• A description of what feature(s) your function illustrates.
• The code and its output must be explained technically.
• The explanation can be provided before or after the code, or in the form of comments within the
code.
• Here is a function that calculates the total cost of a customer's purchase based on the given
scenario:

function calculateTotalCost(item1, item2, item3) {

let totalCost = 0;

let comboDiscount = 0;

let giftDiscount = 0;

// Calculate the total cost of individual items

totalCost += item1 + item2 + item3;

// Check if the customer is purchasing a combo pack

if ((item1 > 0 && item2 > 0 && item3 === 0) || (item1 > 0 && item3 > 0 && item2 === 0) ||
(item2 > 0 && item3 > 0 && item1 === 0)) {

comboDiscount = (totalCost * 10) / 100;

// Check if the customer is purchasing a gift pack

if (item1 > 0 && item2 > 0 && item3 > 0) {

giftDiscount = (totalCost * 25) / 100;

// Apply the discounts to the total cost

totalCost -= comboDiscount;

totalCost -= giftDiscount;

return totalCost;

}
The function is designed to accept three parameters, each representing the quantity of specific items being
bought. Initially, it computes the total cost of the individual items. Subsequently, it verifies whether the
customer is purchasing a combo pack or a gift pack, and subsequently applies the relevant discount to the
total cost. Ultimately, the function returns the total cost after all applicable discounts have been factored
in.
Here is an example usage of the function:

let item1 = 10;

let item2 = 5;

let item3 = 3;

let totalCost = calculateTotalCost(item1, item2, item3);

console.log("Total cost: $" + totalCost);

In this instance, the client is acquiring 10 units of item 1, 5 units of item 2, and 3 units of item 3. The
function is designed to compute the overall expense of these items, and implement any relevant price
reductions. The ultimate total cost will be displayed on the console.

References
Codevscolor, C. (2020, July 6). Python program to find the circumference of a circle.
https://www.codevscolor.com/python-find-circumference-circle

Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press.
https://greenteapress.com/thinkpython2/thinkpython2.pdf

Megamind. (2018). Python program to calculate discount. Posts - OneCompiler.


https://onecompiler.com/posts/3tgw6d4rw/python-program-to-calculate-discount

shecode. (2023). Python function to create a catalog with different pricing rules.
https://www.shecodes.io/athena/74098-python-function-to-create-a-catalog-with-different-
pricing-rules

You might also like