Example
Input
1
Output
Monday
Required to Know
Use of Variables, Use of different operators like assignment or arithmetic operators, Use of methods like System. out. print(), and use of if else, and Use of Data types
Steps
This program determines what day of the week will be based on the input of the user. Here's a breakdown of how it works:
Imports the Scanner class from the java.util package
Defines a public class called Mavenproject1
Defines a main method that takes an array of Strings as input
Creates a new Scanner object called sc with the argument System.in
Reads an integer input from the user using the Scanner object
Uses an if-else structure to print out a string that indicates which day of the week corresponds to the input number.
Program
import java.util.Scanner;
public class Mavenproject1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int day = sc.nextInt();
if(day==1){
System.out.print("First day of the week is monday");
}
else if(day==2){
System.out.print("Second day of the week is tuesday");
}
else if(day==3){
System.out.print("Third day of the week is wednesday");
}
else if(day==4){
System.out.print("Fourth day of the week is thursday");
}
else if(day==5){
System.out.print("Fifth day of the week is friday");
}
else if(day==6){
System.out.print("Sixth day of the week is saturday");
}
else{
System.out.print("Last day of the week is sunday");
}
}
}