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 = date.split('/')
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)
www.leelasoft.com 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.
www.leelasoft.com 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.
www.leelasoft.com 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))
www.leelasoft.com 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.
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
3. Print the temperature in Fahrenheit.
Runtime Test Cases
Case 1:
Enter the temperature in celcius:32
Temperature in Fahrenheit is: 89.6
Case 2:
Enter the temperature in celcius:48
Temperature in Fahrenheit is: 118.4
Python Program to Compute Prime Factors of an Integer
Problem Description
The program takes a number and computes the prime factors of the integer.
Problem Solution
1. Take the value of the integer and store in a variable.
2. Using a while loop, first obtain the factors of the number.
3. Using another while loop within the previous one, compute if the factors are prime or
not.
4. Exit.
Program/Source Code
Here is source code of the Python Program to compute prime factors of an integer. The program
output is also shown below.
n = int(input("Enter an integer:"))
print("Factors are:")
i = 1
while(i <= n):
k = 0
if(n % i == 0):
j = 1
while(j <= i):
if(i % j == 0):
k = k + 1
j = j + 1
if(k == 2):
print(i)
i = i + 1
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Program Explanation
1. User must first enter the value and store it in a variable.
2. The while loop is used and the factors of the integer are computed by using the modulus
operator and checking if the remainder of the number divided by i is 0.
3. Then the factors of the integer are then again checked if the factor is prime or not.
4. If the factor of the integer has two factors, the factor is prime.
5. The prime factor of the integer is printed.
Runtime Test Cases
Case 1:
Enter an integer:25
Factors are:
5
Case 2:
Enter an integer:200
Factors are:
2
5
Python Program to Generate all the Divisors of an Integer
Problem Description
The program takes a number and generates all the divisors of the number.
Problem Solution
1. Take the value of the integer and store it in a variable.
2. Use a for loop and if statement to generate the divisors of the integer.
3. Print the divisors of the number.
4. Exit.
Program/Source Code
Here is source code of the Python Program to generate all the divisors of an integer. The program
output is also shown below.
n = int(input("Enter an integer:"))
print("The divisors of the number are:")
for i in range(1, n + 1):
if(n % i == 0):
print(i)
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Program Explanation
1. User must first enter the value and store it in a variable.
2. Use a for loop to generate numbers from 1 to n.
3. Using an if statement check if the number divided by i gives the remainder as 0 which is
basically the divisor of the integer.
4. Print the divisors of the number.
Runtime Test Cases
Case 1:
Enter an integer:25
The divisors of the number are:
1
5
25
Case 2:
Enter an integer:20
The divisors of the number are:
1
2
4
5
10
20
Python Program to Print Table of a Given Number
Problem Description
The program takes in a number and prints the table of a given number.
Problem Solution
1. Take in a number and store it in a variable.
2. Print the multiplication tables of a given number.
3. Exit.
Program/Source Code
Here is source code of the Python Program to print the table of a given number. The program
output is also shown below.
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
n = int(input("Enter the number to print the tables for:"))
for i in range(1, 11):
print(n, "x", i, "=", n * i)
Program Explanation
1. User must enter a number.
2. Using a print statement, print the multiplication tables of the given number.
Runtime Test Cases
Case 1:
Enter the number to print the tables for:7
7x1=7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Case 2:
Enter the number to print the tables for:17
17 x 1 = 17
17 x 2 = 34
17 x 3 = 51
17 x 4 = 68
17 x 5 = 85
17 x 6 = 102
17 x 7 = 119
17 x 8 = 136
17 x 9 = 153
17 x 10 = 170
Python Program to Print Sum of Negative Numbers, Positive Even Numbers and Positive Odd
numbers in a List
Problem Description
The program prints the sum of negative numbers, positive even numbers and positive odd
numbers in a given list..
www.leelasoft.com Cell: 78 42 66 47 66