Leela Soft 1000 MCQ Programs Madhu
2. Python Programming Examples on Mathematical Functions
Python Program to Check if a Date is Valid and Print the Incremented Date if it is
Problem Description
The program takes in a date and checks if it a valid date and prints the incremented date if it is.
Problem Solution
1. Take in the date of the form: dd/mm/yyyy.
2. Split the date and store the day, month and year in separate variables.
3. Use various if-statements to check if the day, month and year are valid.
4. Increment the date if the date is valid and print it
5. Exit.
Program/Source Code
Here is source code of the Python Program to check if a date is valid and print the incremented
date if it is. The program output is also shown below.
date = input("Enter the date in the format dd/mm/yyyy :")
dd, mm, yy = [Link]('/')
dd = int(dd)
mm = int(mm)
yy = int(yy)
if(mm == 1 or mm == 3 or mm == 5 or mm == 7 or mm == 8 or mm == 10 or mm == 12):
max1 = 31
elif(mm == 4 or mm == 6 or mm == 9 or mm == 11):
max1 = 30
elif(yy % 4 == 0 and yy % 100 != 0 or yy % 400 == 0):
max1 = 29
else:
max1 = 28
if(mm < 1 or mm > 12):
print("Date is invalid.")
elif(dd < 1 or dd > max1):
print("Date is invalid.")
elif(dd == max1 and mm != 12):
dd = 1
mm = mm + 1
print("The incremented date is: ", dd, mm, yy)
elif(dd == 31 and mm == 12):
dd = 1
mm = 1
yy = yy + 1
print("The incremented date is: ", dd, mm, yy)
else:
dd = dd + 1
print("The incremented date is: ", dd, mm, yy)
[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Program Explanation
1. User must enter the date of the form dd/mm/yy.
2. The date is then split and the day, month and year is stored in separate variables.
3. If the day isn’t between 1 and 30 for the months of April, June, September and November,
the date is invalid.
4. If the day isn’t between 1 and 31 for the months January, March, April, May, July, August,
October and December, the date is invalid.
5. If the month is February, the day should be between 1 and 28 for years other than the
leap year and between 1 and 29 for leap years.
6. If the date is valid, the date should be incremented.
7. The final result is printed.
Runtime Test Cases
Case 1
Enter the date: 5/7/2016
The incremented date is: 6 7 2016
Case 2
Enter the date: 30/2/1997
Date is invalid.
Case 3
Enter the date: 31/12/2016
The incremented date is: 1 1 2017
Python Program to Compute Simple Interest Given all the Required Values
Problem Description
The program computes simple interest given the principle amount, rate and time.
Problem Solution
1. Take in the values for principle amount, rate and time.
2. Using the formula, compute the simple interest.
3. Print the value for the computed interest.
4. Exit.
Program/Source Code
Here is source code of the Python Program to compute simple interest given all the required
values. The program output is also shown below.
[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
principle = float(input("Enter the principle amount:"))
time = int(input("Enter the time(years):"))
rate = float(input("Enter the rate:"))
simple_interest = (principle * time * rate) / 100
print("The simple interest is:", simple_interest)
Program Explanation
1. User must enter the values for the principle amount, rate and time.
2. The formula: (amount*time*rate)/100 is used to compute simple interest.
3. The simple interest is later printed.
Runtime Test Cases
Case 1:
Enter the principle amount:200
Enter the time(years):5
Enter the rate:5.0
The simple interest is: 50.0
Case 2:
Enter the principle amount:70000
Enter the time(years):1
Enter the rate:4.0
The simple interest is: 2800.0
Python Program to Check Whether a Given Year is a Leap Year
Problem Description
The program takes in a year and checks whether it is a leap year or not.
Problem Solution
1. Take the value of the year as input
2. Using an if-statement, check whether the year is a leap year or not
3. Print the final result
4. Exit
Program/Source Code
Here is source code of the Python Program to Calculate the Average of Numbers in a Given List.
The program output is also shown below.
[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
year = int(input("Enter year to be checked:"))
if(year % 4 == 0 and year % 100 != 0 or year % 400 == 0):
print("The year is a leap year!")
else:
print("The year isn't a leap year!")
Program Explanation
1. User must first enter the year to be checked.
2. The if statement checks if the year is a multiple of 4 but isn’t a multiple of 100 or if it is a
multiple of 400 (not every year that is a multiple of 4 is a leap year).
3. Then the result is printed.
Runtime Test Cases
Case 1:
Enter year to be checked:2016
The year is a leap year!
Case 2:
Enter year to be checked:2005
The year isn't a leap year!
Python Program to Read Height in Centimeters and then Convert the Height to Feet and Inches
Problem Description
The program reads the height in centimeters and then converts the height to feet and inches.
Problem Solution
1. Take the height in centimeters and store it in a variable.
2. Convert the height in centimeters into inches and feet.
3. Print the length in inches and feet.
4. Exit.
Program/Source Code
Here is source code of the Python Program to Calculate the Average of Numbers in a Given List.
The program output is also shown below.
cm = int(input("Enter the height in centimeters:"))
inches = 0.394 * cm
feet = 0.0328 * cm
print("The length in inches", round(inches, 2))
print("The length in feet", round(feet, 2))
[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Program Explanation
1. User must enter the height in centimeters.
2. The height in centimeters is multiplied by 0.394 and stored in another variable which now
contains the height in inches.
3. The height in centimeters is multiplied by 0.0328 and stored in another variable which
now contains the height in feet.
4. The converted height in inches and feet is printed.
Runtime Test Cases
Case 1:
Enter the height in centimeters:50
The length in inches 19.7
The length in feet 1.64
Case 2:
Enter the height in centimeters:153
The length in inches 60.28
The length in feet 5.02
Python Program to Take the Temperature in Celcius and Covert it to Farenheit
Problem Description
The program takes the temperature in Celsius and converts it to Fahrenheit.
Problem Solution
1. Take the value of temperature in Celsius and store it in a variable.
2. Convert it to Fahrenheit.
3. Print the final result.
4. Exit.
Program/Source Code
Here is source code of the Python Program to take the temperature in Celsius and convert it to
Fahrenheit. The program output is also shown below.
celsius = int(input("Enter the temperature in celcius:"))
f = (celsius * 1.8) + 32
print("Temperature in farenheit is:", f)
Program Explanation
1. User must first enter the value of temperature in Celsius.
2. Using the formula of: f=(c*1.8)+32, convert Celsius to Fahrenheit.
[Link] Cell: 78 42 66 47 66