Program to find the number of days in a given month of a given year | faceprep

Program to find the number of days in a given month of a given year is discussed here. The month and year are given as input, the number of days in that month is displayed as output.

Sample Input:

3
1996

Sample Output:

Number of days is 31

Algorithm to find the number of days in a given month of a given year

  • Input the month and year from the user.
  • If the year is a leap year and (month == 2), display “Number of days is 29”.
  • Else if (month == 2), display “Number of days is 28”.
  • Else if((month == 1) ||¬†(month == 3 || (month == 5) (month == 7) || (month == 8) || (month == 10) (month == 12)), display “Number of days is 31”.
  • Else, display, “Number of days is 30”.

Program to find the number of days in a given month of a given year is given below.

// C program to find the number of days in a given month

#include<stdio.h>
int main()
{
//fill the code
int year, month;
scanf(“%d %d”,&month,&year);


if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
printf(“Number of days is 31”);
else if((month == 2) && (year%4 == 0) || ((year%100 == 0) &&(year%400 == 0)))
{
printf(“Number of days is 29”);
}
else if(month == 2)
{
printf(“Number of days is 28”);
}
else
printf(“Number of days is 30 “);

return 0;
}

// C++ program to find the number of days in a given month

#include<iostream>
using namespace std;

int main()
{
//fill the code
int year, month;
cin >> month >> year;


if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
cout << “Number of days is 31”;
else if((month == 2) && (year%4 == 0) || ((year%100 == 0) &&(year%400 == 0)))
{
cout << “Number of days is 29”;
}
else if(month == 2)
{
cout << “Number of days is 28”;
}
else
cout << “Number of days is 30 “;

return 0;
}

// Java program to find the number of days in a given month

import java.util.*;
public class Main
{
public static void main(String[] args)
{
int year, month;
Scanner sc = new Scanner(System.in);
month = sc.nextInt();
year = sc.nextInt();
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
System.out.print(“Number of days is 31”);
else if((month == 2) && (year%4 == 0) || ((year%100 == 0) &&(year%400 == 0)))
{
System.out.print(“Number of days is 29”);
}
else if(month == 2)
{
System.out.print(“Number of days is 28”);
}
else
System.out.println(“Number of days is 30 “);
}
}

# Python program to find the number of days in a given month

month = int(input())
year = int(input())
if(month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12):
print(“Number of days is 31”)
elif((month == 2) and (year%4 == 0) or ((year%100 == 0) and (year%400 == 0))):

print(“Number of days is 29”)

elif(month == 2):
print(“Number of days is 28”)
else:
print(“Number of days is 30 “)

Output:

find the number of days in a given month of a given year