Write a program in Java to find out week day name using switch statement. The user enters the
day number and the program returns the day name.
Answers
Answer:
int ch;
System.out.println("Enter day no. 1 for Sunday till 7 for Saturday ") ;
ch=sc.nextInt() ;
switch(ch)
{
case 1:System.out.println(" Sunday ") ;
break;
case 2:System.out.println(" Monday") ;
break;
case 3:System.out.println("Tuesday") ;
break;
case 4:System.out.println("Wednesday") ;
break;
case 5:System.out.println("Thursday") ;
break;
case 6:System.out.println("Friday") ;
break;
case 7:System.out.println("Saturday") ;
break;
default:System.out.println("Invslid choice ") ;
}
Question:-
- Write a program in Java to find out week day name using switch statement. The user enters the day number and the program returns the day name.
Program:-
import java.util.*;
class Day
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter day number: ") ;
int x=sc.nextInt() ;
switch(x)
{
case 1:System.out.println("Sunday. ") ;
break;
case 2:System.out.println("Monday. ") ;
break;
case 3:System.out.println("Tuesday.") ;
break;
case 4:System.out.println("Wednesday.") ;
break;
case 5:System.out.println("Thursday.") ;
break;
case 6:System.out.println("Friday.") ;
break;
case 7:System.out.println("Saturday.") ;
break;
default:System.out.println("Invalid choice. Please try again later. ") ;
}
}
}