strings and variable

Problems on variable and strings

January 12, 20243 min read

Problem 1: Basic Interest Calculation

Description: Write a Python program to calculate the interest on a given investment using the formula interest = principal * rate * time. The program should take the principal amount, annual interest rate, and time in years as inputs.

Sample Input:

principal = 10000
rate = 0.05
time = 5

Expected Output:

Interest = 2500.0

Problem 2: Formatting Currency

Description: Write a Python program to format a given amount into a string with a currency symbol. For instance, formatting 5200 as "$5200.00".

Sample Input:

amount = 5200

Expected Output:

"$5200.00"

Problem 3: Concatenating Financial Strings

Description: Write a Python program to concatenate two strings representing financial data. Ensure the concatenated string is properly formatted.

Sample Input:

string1 = "Total Income: $"
income = 7500

Expected Output:

"Total Income: $7500"

Problem 4: Swapping Values of Two Variables

Description: Write a Python program to swap the values of two variables without using a temporary third variable. This exercise demonstrates a Pythonic way to exchange values and emphasizes the importance of tuple unpacking in Python.

Sample Input:

x = 5
y = 10

Expected Output:

"Values after swapping: x = 10, y = 5"

Problem 5: Parsing Stock Price

Description: Write a Python program to parse a string representing a stock price and convert it into a float. Ensure the program handles a dollar sign and commas correctly.

Sample Input:

stock_price = "$1,234.56"

Expected Output:

1234.56

Problem 6: Loan Repayment Calculator

Description: Calculate the monthly repayment amount for a loan using the formula repayment = (principal * monthly_rate) / (1 - (1 + monthly_rate) ** -months). Assume monthly compounding.

Sample Input:

principal = 50000
annual_rate = 0.07
years = 5

Expected Output:

Monthly Repayment = 990.55

Problem 7: Compound Interest Calculator

Description: Write a Python program to calculate compound interest using the formula A = P * (1 + r/n) ** (n*t), where P is the principal amount, r is the annual interest rate, t is time in years, and n is the number of times interest is applied per time period.

Sample Input:

principal = 10000
rate = 0.08
time = 5
compoundings_per_year = 4

Expected Output:

Amount = 14859.47

Problem 8: Currency Conversion

Description: Write a program that converts a given amount in USD to another currency at a specified exchange rate.

Sample Input:

usd_amount = 1000
exchange_rate = 0.85  # USD to EUR

Expected Output:

"EUR Amount = 850.00"

Problem 9: Stock Gain/Loss Calculation

Description: Calculate the gain or loss from selling a stock.

Sample Input:

purchase_price = 20.50
selling_price = 25.75
shares = 100

Expected Output:

"Profit = 525.00"

Problem 10: Break-even Point in Sales

Description: Calculate the break-even point in units for a product. Use the formula break_even_units = fixed_costs / (price_per_unit - variable_cost_per_unit).

Sample Input:

fixed_costs = 10000
price_per_unit = 50
variable_cost_per_unit = 30

Expected Output:

"Break-even Units = 500"

Problem 11: Percentage Change in Stock Price

Description: Calculate the percentage change in a stock price.

Sample Input:

old_price = 150
new_price = 165

Expected Output:

"Percentage Change = 10.00%"

Problem 12: Gross Profit Margin Calculator

Description: Calculate the gross profit margin. Use the formula gross_profit_margin = (gross_profit / revenue) * 100.

Sample Input:

gross_profit = 20000
revenue = 50000

Expected Output:

"Gross Profit Margin = 40.00%"

Problem 13: Calculating Sales Tax

Description: Write a program to calculate the total cost including sales tax.

Sample Input:

price = 150
sales_tax_rate = 0.07

Expected Output:

"Total Cost = 160.50"

Problem 14: Dividend Yield Calculation

Description: Calculate the dividend yield of a stock. Use the formula dividend_yield = (annual_dividend_per_share / price_per_share) * 100.

Sample Input:

annual_dividend_per_share = 2
price_per_share = 40

Expected Output:

"Dividend Yield = 5.00%"

Problem 15: Approximating a Price Without Using Round Function

Description: Write a Python program to approximate a given floating-point price to two decimal places without using the built-in round() function. The solution should truncate the extra decimal places beyond two digits.

Sample Input:

price = 33.3333333

Expected Output:

"Approximated Price = 33

.33"

blog author image

sunil s

Quant Developer & Mentor

Back to Blog