Write a program to enter the number of month and print the name of the month using switch case statement
Answers
Explanation:
Hence, write 12 cases inside switch and one default case as else block. Print 31 for case 1, 3, 5, 7, 8, 10, 12 . Print 30 for case 4, 6, 9, 11 . Print 28/29 for case 2 .
...
Logic to print number of days in month using switch... case.
Month Total days
February 28/29 days
April, June, September, November.
import java.util.*;
class Hello
{
public static void main (string args [])
{
Scanner sc=new Scanner(System .in);
int d;
System.out.println("Enter a month no.");
d=sc.nextInt();
switch=(d)
{
Case1: System.out.println("January");
break;
Case2: System.out.println("February");
break;
Case3: System.out.println("March");
break;
Case4: System.out.println("April");
break;
Case5: System.out.println("May");
break;
Case6: System.out.println("June");
break;
Case7: System.out.println("July");
break;
Case8: System.out.println("August");
break;
Case9: System.out.println("September");
break;
Case10: System.out.println("October");
break;
Case11: System.out.println("November");
break;
Case12: System.out.println("December");
break;
default: System.out.println("Wrong Month");
}
}
}