Write a program that prompts the user to enter the month and year and displays t
ID: 3629088 • Letter: W
Question
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.Here is a sample run of this program:
Enter a month in the year (e.g., 1 for Jan): 1
Enter a year: 2009
January 2009 has 31 days
Exercise 3.11
Liang, Y. Daniel. Introduction to Java Programming (8th Edition)
Explanation / Answer
please rate - thanks
import java.util.*;
public class daysinmonth
{
public static void main(String[] args)
{Scanner in = new Scanner(System.in);
int month,year,days=0;
String name;
System.out.print("Enter year: ");
year=in.nextInt();
System.out.print("Enter month(1 = January, 12 = December): ");
month=in.nextInt();
if(month<1||month>12)
System.out.println("InValid date");
else
{if(month==1)
{days=31;
name="January";
}
else if(month==2)
{days=28;
name="February";
if(year%4==0)
if(year%100==0)
{if(year%400==0)
days++;
}
else
days++;
}
else if(month==3)
{days=31;
name="March";
}
else if(month==4)
{days=30;
name="April";
}
else if(month==5)
{days=31;
name="May";
}
else if(month==6)
{days=30;
name="June";
}
else if(month==7)
{days=31;
name="July";
}
else if(month==8)
{days=31;
name="August";
}
else if(month==9)
{days=30;
name="September";
}
else if(month==10)
{days=31;
name="October";
}
else if(month==11)
{days=30;
name="November";
}
else if(month==12)
{days=31;
name="December";
}
}
System.out.println(name +" "+year+" has "+days+" days");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.