Computer Science, asked by pandusweety1998, 1 year ago

write a python code to generate the next date of a given date​

Answers

Answered by pragatibhatt2922
0

Answer:

Explanation:

year = int(input("Input a year: "))

if (year % 400 == 0):

  leap_year = True

elif (year % 100 == 0):

  leap_year = False

elif (year % 4 == 0):

  leap_year = True

else:

  leap_year = False

month = int(input("Input a month [1-12]: "))

if month in (1, 3, 5, 7, 8, 10, 12):

  month_length = 31

elif month == 2:

  if leap_year:

      month_length = 29

  else:

      month_length = 28

else:

  month_length = 30

day = int(input("Input a day [1-31]: "))

if day < month_length:

  day += 1

else:

  day = 1

  if month == 12:

      month = 1

      year += 1

  else:

      month += 1

print("The next date is [dd-mm-yyyy] %d-%d-%d." % (day, month, year))

Python program to generate and display the next date of a given date.

Language using : Python Programming

Program :

day=int(input("Enter the day :"))

month=int(input("Enter the month :"))

year=int(input("Enter the year :"))

if year%400==0 or (year%4==0 and year%100!=0):

  #leap year

  if month==2:

      no_of_days=29

  elif month in [4,6,9,11]:

      no_of_days=30

  else:

      no_of_days=31

else:

  #not a leap year

  if month==2:

      no_of_days=28

  elif month in [4,6,9,11]:

      no_of_days=30

  else:

      no_of_days=31

if day<no_of_days:

  print(day+1,"-",month,"-",year)

else:

  if month==12:

      print(1,"-",1,"-",year+1)

  else:

      print(1,"-",month+1,"-",year)

Inputs and outputs with various test cases :

Input 1: (General test)

Enter the day :1

Enter the month :9

Enter the year :2015

Output 1:

2 - 9 - 2015

Input 2: (Last date of an year)

Enter the day :31

Enter the month :12

Enter the year :2000

Output 2:

1 - 1 - 2001

Input 3: (Leap year, february checking)

Enter the day :28

Enter the month :2

Enter the year :2000

Output 3:

29 - 2 - 2000

Input 4: (Not a leap year, Century year, february checking)

Enter the day :28

Enter the month :2

Enter the year :1900

Output 4:

1 - 3 - 1900

Explanation :

First, we need to find out whether the given year is leap year or not. If yes, we need to make the no.of days on february to 29; else 28. Similarly, on listing the 30 day months(4), we need to update their month lengths to 30 (because they are less in numer, so, it decreases time complexity, compared to 31 day months (7)), and all other months length should be filled with 31.

On finding year type and month, we need to go with date. If date is less than the specified month, just incrementing te date by 1 makes the work done. else, the date will be equal to month length. Then we need to make day as 1, incrementing the month, leaving year the same. If the month is 12 and date is 31, update the date and month to 1, and increment year by 1. That's it.

Note : I have added the attachments of the interpretation and the execution of programs with their outputs. Take a look!

Quick Tip : In python, you must and should follow Indentation. Following the indentation property in any programming language will help you in quickly analysing the code, if an error occurs.

Similar questions