Write a program to input week number and print week day


The program is a Java program that takes a week number input from the user and prints the corresponding day of the week.

  • The program starts by importing the java.util.Scanner package which allows user input to be taken from the console.
  • In the main method, the program prompts the user to enter a week number between 1 and 7 using the System.out.print method and reads the input using the Scanner object's nextInt() method.
  • The program then checks the entered week number using a series of if and else if statements. If the entered week number is between 1 and 7, the program prints the corresponding day of the week using the System.out.println() method.
  • If the entered week number is not between 1 and 7, the program prints "Enter 1 to 7..." using the System.out.println() method.
  • Finally, the program exits the main method, and the program execution ends.

Source Code

import java.util.Scanner;
class WeekDay
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the Week Number(1-7) :");
		int num = input.nextInt();			
		if(num==1)
			System.out.println("This is a Sunday");
		else if(num==2)
			System.out.println("This is a Monday");
		else if(num==3)
			System.out.println("This is a Tuesday");
		else if(num==4)
			System.out.println("This is a Wednesday");
		else if(num==5)
			System.out.println("This is a Thursday");
		else if(num==6)
			System.out.println("This is a Friday");
		else if(num==7)
			System.out.println("This is a Saturday");
		else
			System.out.println("Enter 1 to 7...");
	}
}

Output

Enter the Week Number(1-7) :5
This is a Thursday

Example Programs