A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write a Java program to find the number of days in a month
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)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now