# Java Program to Find the number of days in a Months.

In this post, we will write a two java program to Find the Number of days in months using the If-else statement, and Switch Case.

## How the program will work?

• if month number is 1 than its January and 31 days
• if month number is 2 than its February check year is leap year and 28/29 days.
• so on.

### Example:

1. Input:
2. Enter month number:2
3. Enter the year: 2016
4. Output:
5. February 2016 has 29 days

## Example 1: Program to find days from month and year using if-else

This program asks the user to enter Month number between 1 to 12 and Year , where January =1 , and February=2,and so on. Based on the user input value, the program print the number of days in the particular month. To accomplish this goal, we are using If-else statement.

```import java.util.Scanner;
public class NumberOfDaysInMonths {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int daysinMonth=0;
String MonthName = "Invalid";

System.out.println("Enter the month number: ");
int month = sc.nextInt();

System.out.println("Enter the Year: ");
int year = sc.nextInt();

if(month==1){
MonthName = "January";
daysinMonth = 31;
}
else if (month==2){
MonthName = "February";
if((year%400==0) || ((year%100!=0) && (year%4==0))){
daysinMonth = 29;
}
else {
daysinMonth = 28;
}
}
else if(month==3){
MonthName = "March";
daysinMonth = 31;
}
else if(month==4){
MonthName = "April";
daysinMonth = 30;
}
else if(month==5){
MonthName = "May";
daysinMonth = 31;
}else if(month==6){
MonthName = "June";
daysinMonth = 30;
}else if(month==7){
MonthName = "July";
daysinMonth = 31;
}else if(month==8){
MonthName = "August";
daysinMonth = 31;
}else if(month==9){
MonthName = "September";
daysinMonth = 30;
}else if(month==10){
MonthName = "October";
daysinMonth = 31;
}else if(month==11){
MonthName = "November";
daysinMonth = 30;
}else if(month==12){
MonthName = "December";
daysinMonth = 31;
}

System.out.println(MonthName+" "+year+" has "+daysinMonth+" days\n");
}
}
```

### Output:

```Enter month number:2
Enter the year: 2016
February 2016 has 29 days```

## Example 2: Program to find days from month and year using switch-case

In this program, we will deal with multiple conditions. In this Java program, we are using the switch Case to return the number of days in a months.

```import java.util.Scanner;
public class NumberOfDaysInMonths {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int daysinMonth=0;
String MonthName = "Invalid";

System.out.println("Enter the month number: ");
int month = sc.nextInt();

System.out.println("Enter the Year: ");
int year = sc.nextInt();

switch (month) {

case 1:
MonthName = "January";
daysinMonth = 31;
break;
case 2:
MonthName = "February";
if ((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0))) {
daysinMonth = 29;
break;
} else {
daysinMonth = 28;
break;
}
case 3:
MonthName = "March";
daysinMonth = 31;
break;
case 4:
MonthName = "April";
daysinMonth = 30;
break;
case 5:
MonthName = "May";
daysinMonth = 31;
break;
case 6:
MonthName = "June";
daysinMonth = 30;
break;
case 7:
MonthName = "July";
daysinMonth = 31;
break;
case 8:
MonthName = "August";
daysinMonth = 31;
break;
case 9:
MonthName = "September";
daysinMonth = 30;
break;
case 10:
MonthName = "October";
daysinMonth = 31;
break;
case 11:
MonthName = "November";
daysinMonth = 30;
break;
case 12:
MonthName = "December";
daysinMonth = 31;
break;
}
System.out.println(MonthName+" "+year+" has "+daysinMonth+" days\n");
}
}```

### Output:

```Enter month number:10
Enter the year: 2019
October 2016 has 30 days```