Q:

Write a Java program to find the number of days in a month

0

Write a Java program to find the number of days in a month

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.Scanner;
public class JavaExcercise {
 
 
  public static void main(String[] strings) {
 
        Scanner input = new Scanner(System.in);
 
        int numberOfDaysInMonth = 0; 
        String MonthName = "Unknown";
 
        System.out.print("Input a month number: ");
        int month = input.nextInt();
 
        System.out.print("Input a year: ");
        int year = input.nextInt();
 
        switch (month) {
            case 1:
                MonthName = "January";
                numberOfDaysInMonth = 31;
                break;
            case 2:
                MonthName = "February";
                if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
                    numberOfDaysInMonth = 29;
                } else {
                    numberOfDaysInMonth = 28;
                }
                break;
            case 3:
                MonthName = "March";
                numberOfDaysInMonth = 31;
                break;
            case 4:
                MonthName = "April";
                numberOfDaysInMonth = 30;
                break;
            case 5:
                MonthName = "May";
                numberOfDaysInMonth = 31;
                break;
            case 6:
                MonthName = "June";
                numberOfDaysInMonth = 30;
                break;
            case 7:
                MonthName = "July";
                numberOfDaysInMonth = 31;
                break;
            case 8:
                MonthName = "August";
                numberOfDaysInMonth = 31;
                break;
            case 9:
                MonthName = "September";
                numberOfDaysInMonth = 30;
                break;
            case 10:
                MonthName = "October";
                numberOfDaysInMonth = 31;
                break;
            case 11:
                MonthName = "November";
                numberOfDaysInMonth = 30;
                break;
            case 12:
                MonthName = "December";
                numberOfDaysInMonth = 31;
        }
        System.out.print(MonthName + " " + year + " has " + numberOfDaysInMonth + " days\n");
    }
}

Result:

Input a month number: 2

Input a year: 2019

February 2019 has 28 days

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a Java program to test a number is positive ... >>
<< Write a Java program to take three numbers from th...