Wap to find no. Of days in a month taking month and year from user. Program of python
Answers
Heya friend,
Let's examine this first :
We will consider the month number as follows :
January - 1
February - 2 ...
till December - 12
So, we'll take the umber of month from the user.
Now, the special case is when the number 2 , i.e. the month February comes.
We know in leap year, it has 29 days and otherwise it has 28 days, and,
A leap year comes in every 4 year, so we can check that year by dividing it by 4, and if so, it gives the remainder as 0, it is a leap year, otherwise it is not.
Now, let's move on to the code:
SOURCE CODE
# To check number of days in a given month
m=int(input('Enter the number of the month : '))
if m in [1,3,5,7,8,10,12] :
print('There are 31 days in this month')
elif m==2:
y=int(input('Enter the year :'))
if y%4==0:
print('There are 29 days in this month')
else:
print('There are 28 days in this month')
elif m in [4,6,9,11]:
print('There are 30 days in this month')
else:
print('Invalid character : Enter a valid number')
Now, I'll be showing the output for all the 5 cases :
OUTPUT
Enter the number of the month : 1
There are 31 days in this month
>>>
>>>
Enter the number of the month : 4
There are 30 days in this month
>>>
>>>
Enter the number of the month : 2
Enter the year :2004
There are 29 days in this month
>>>
Enter the number of the month : 2
Enter the year :2005
There are 28 days in this month
>>>
>>>
Enter the number of the month : 13
Invalid character : Enter a valid number
>>>