Days Before N Days in Python

In the previous article we explored the sum of Natural Numbers in Python. Suppose we are given some days, each number means a successive different day for example: Sunday represents 0, 1 represents Monday, 2 represents Tuesday, 3 represents Wednesday, 4 represents Thursday, 5 represents Friday, 6 represents Saturday. The program calculates the number of days left until a specific particular day.

Implementation of Days Before N Days in Python

Such a program’s implementation in another common languages would be much more complex and likely rely on huge looping statements, Python offers special syntax that allows us to reduce the code needed. In normal formulaic terms for solving the problem we can express it in a case where d is 0 and n is 9 as:

(0 – 9 ) % 7
(5 – 2*7) % 7

To implement the logic into python we would need to accept two values, for d & n respectively:

#Program to estimate the number of days before a day
d=int(input("Enter d: "))
n=int(input("Enter n: "))
print((d-n) % 7)
Output:
Enter d: 0
Enter n: 9
5

In the next article we will look at the program for Finding the Last Digit in Python

Leave a Comment