Programming Exercises

  1. Temperature Converter

Write a program that asks the user to enter a temperature in Celsius, then converts that temperature to Fahrenheit using a function. The conversion formula is as follows:

C to F function

Create and use a function for the conversion.

 

  1. Compound Interest Calculator

Write a program that calculates the compound interest earned on an investment. The program should ask for the principal amount (P), the annual interest rate (r), the number of years (t), and the number of times interest is applied per time period (n) during the investment period.

Use the following formula for compound interest:

Compound interest function

  • A – final Amount
  • P – principal Amount
  • r – annual interest rate
  • n – number of times interest applied per time period
  • t – number of time periods (years) elapsed

Create and use a function to perform the compound interest calculation.

 

  1. Body Mass Index (BMI) Calculator

Write a program that asks the user to enter their weight in kilograms and height in meters. The program should then calculate and display the user's BMI using a function.

The formula for BMI is:

BMI formula

 

  1. Grade Average Calculator

Write a program that asks the user to enter five test scores. The program should calculate the average of the test scores using a function and display it. Implement the calculation and display logic in separate functions.

 

  1. Tip Calculator

Write a program that asks the user to enter the cost of a meal at a restaurant. The program should then calculate and display the tip amounts for 15%, 18%, and 20% of the meal cost using functions. Create and use a function to calculate the tip for a given percentage.

 

  1. Leap Year Checker

Write a program that asks the user to enter a year. The program should then determine whether the year is a leap year using a function. A year is a leap year if it is divisible by 4, but not divisible by 100, unless it is also divisible by 400.

 

  1. Retail Price Calculator

Write a program that asks the user to enter the wholesale cost of an item and its markup percentage. The program should calculate and display the retail price of the item using a function.

The formula is:

Retail price formula

 

  1. Fibonacci Sequence Generator

Write a program that asks the user for a number n and then generates and displays the first n numbers in the Fibonacci sequence using a function. Implement the sequence generation in a function and use it to produce the output.

 

  1. Grade Calculator

Write a program that asks the user to enter a score out of 100. The program should then display the corresponding letter grade using a function based on the following scale:

  • A: 90-100
  • B: 80-89
  • C: 70-79
  • D: 60-69
  • F: 0-59 Create and use a function to determine the letter grade.

 

  1. Prime Number Checker

Write a program that asks the user to enter a number. The program should then determine whether the number is a prime number using a function.

Implement the prime-checking logic in a function and use it to check the input number.