Write a Java program to accept numbers 1-7. If user will enter no. 1, it should
display 'Monday' on the screen. Like this do it for all week-days. Display
invalid choice if user will enter any another number.
(Write with if-else if construct)
Answers
Program :-
import java.util.Scanner; \\importing package
class d \\class declaration
{
public static void main(String args[]) \\main()method declaration
{
System.out.println("Enter a number between 1-7 to get dayname");
int n; \\variable declaration
Scanner sc=new Scanner(System.in); \\input object creation
n = sc.nextInt();
if(n==1)
{
System.out.println("Monday");
}
else if(n==2)
{
System.out.println("Tuesday");
}
else if(n==3)
{
System.out.println("Wednesday");
}
else if(n==4)
{
System.out.println("Thrusday");
}
else if(n==5)
{
System.out.println("Friday");
}
else if(n==6)
{
System.out.println("Saturday");
}
else if(n==7)
{
System.out.println("Sunday");
}
else
{
System.out.println("Entered invalid number");
}
}
}
Output :- is in the attachment.
Glossary :-
More Programs to practice :-
Q. Write a Java program to accept numbers 1-12. If user will enter no. 1, it should display 'January' on the screen. Like this do it for all month-days. Display invalid choice if user will enter any another number.
(Write with if-else if construct)