Write A Java Program To Find Number Of Days In A Month
In this Java tutorial, we will write a Java Program to find the number of days in a month using the else if statement and the switch case with examples.
Java Program to Find Number of Days in a Month using Else If
Let’s write a java code to find the number of days in a month using the else if statement.
The total number of days in various months are :
- January, March, May, August, October, and December = 31 Days
- February = 28 or 29 Days
- April, June, September, and November = 30 Days
import java.util.Scanner; public class DaysinMonth { private static Scanner sc; public static void main(String[] args) { int month; sc = new Scanner(System.in); System.out.print(" Please Enter The Month Number from 1 to 12 (1 = Jan, and 12 = Dec) : "); month = sc.nextInt(); if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ) { System.out.println("\n 31 Days in this Month"); } else if ( month == 4 || month == 6 || month == 9 || month == 11 ) { System.out.println("\n 30 Days in this Month"); } else if ( month == 2 ) { System.out.println("\n Either 28 or 29 Days in this Month"); } else System.out.println("\n Please enter the Valid Number between 1 to 12"); } }
Java Program to Find Number of Days in a Month using Switch Case
import java.util.Scanner; public class DaysinMonthCase { private static Scanner sc; public static void main(String[] args) { int month; sc = new Scanner(System.in); System.out.print(" Please Enter The Month Number from 1 to 12 (1 = Jan, and 12 = Dec) : "); month = sc.nextInt(); switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: System.out.println("\n 31 Days in this Month"); break; case 4: case 6: case 9: case 11: System.out.println("\n 30 Days in this Month"); break; case 2: System.out.println("\n Either 28 or 29 Days in this Month"); break; default: System.out.println("\n Please enter Valid Number between 1 to 12"); } } }