61.9k views
4 votes
(Find the number of days in a month) Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, if the user entered month 2 and year 2000, the program should display that February 2000 has 29 days. If the user entered month 3 and year 2005, the program should display that March 2005 has 31 days

User Whizzle
by
6.4k points

1 Answer

4 votes

Answer:

// here is code in java.

import java.util.*;

// class definition

public class Main

{

public static void main(String[] args) {

// scanner object to read input from user

Scanner s=new Scanner(System.in);

// array of months

String mon[] ={

null , "January" , "February" , "March" , "April", "May",

"June", "July", "August", "September", "October",

"November", "December"};

// array which store number of days corresponding to months

int days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

// ask user to enter month

System.out.print("enter the month:");

// read month

int m=s.nextInt();

// ask user to enter year

System.out.print("enter the year:");

// read year

int year=s.nextInt();

// check the leap year

if(m==2 &&(((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0)))

{

System.out.println(mon[m]+" "+year+" has 29 days.");

}

else

System.out.println(mon[m]+" "+year+" has "+days[m]+" days");

}

}

Step-by-step explanation:

Declare and initial month array and days array.Then read the month and year From user with the scanner object.Check if entered year is a leap year or not.Then based on the year and month print the number of days .

Output:

enter the month:2

enter the year:2000

February 2000 has 29 days.

enter the month:3

enter the year:2005

March 2005 has 31 days.

User Kudu
by
5.6k points