Java program that prompts the user to enter a month and year and displays the number of days in the month. For example, if the user entered month 2 and year 2012, the program should display that February 2012 had 29 days. If the user entered month 3 and year 2015, the program should display that March 2015 had 31 days.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> */<br /><br />import java.util.Scanner;<br /><br />public class Ex03_11 {<br /><br /> public static void main(String[] args) {<br /> int month, year, days;<br /> Scanner input = new Scanner(System.in);<br /> System.out.print("Enter month eg February = 2: ");<br /> month = input.nextInt();<br /> System.out.print("Enter year: ");<br /> year = input.nextInt();<br /> <br /> if (month == 1)<br /> System.out.println("January " + year + " had 31 days");<br /> if (month == 2 && (year % 4 != 0))<br /> System.out.println("February " + year + " had 28 days");<br /> if (month == 2 && (year % 4 == 0))<br /> System.out.println("February " + year + " had 29 days");<br /> if (month == 3)<br /> System.out.println("March " + year + " had 31 days");<br /> if (month == 4)<br /> System.out.println("April " + year + " had 30 days");<br /> if (month == 5)<br /> System.out.println("May " + year + " had 31 days");<br /> if (month == 6)<br /> System.out.println("June " + year + " had 30 days");<br /> if (month == 7)<br /> System.out.println("July " + year + " had 31 days");<br /> if (month == 8)<br /> System.out.println("August " + year + " had 31 days");<br /> if (month == 9)<br /> System.out.println("September " + year + " had 30 days");<br /> if (month == 10)<br /> System.out.println("October " + year + " had 31 days");<br /> if (month == 11)<br /> System.out.println("November " + year + " had 30 days");<br /> if (month == 12)<br /> System.out.println("December " + year + " had 31 days");<br /> }<br />}<br /> |
Sample run |
Click here to see other solutions to Introduction to Java Programming.