Computer Science, asked by chakori1579, 2 months ago

Write a Java program that keeps a number from the user and generates an integer between 1 and 7 and displays the name of the weekday

Answers

Answered by ShamashriMndl
2

In this post, we are going to take input from a user between 1 to 2 and print the day name base on weeks no. If the user enters 1 that means it is Monday.

We are going to see the two programs. First program using Nested if-else in which value from the user program check the week number using if-else condition and display the corresponding days.

In the second program we are using switch case in which value from the user program check the week number and display the corresponding days.

Example,

Input1

Enter weekdays number: 1

Monday

Input2

Enter weekdays number :4

Thursday

Example 1: Program to display weekday between 1 to 7 using if-else

The below program ask the user to enter the valid week number. After getting the value from the user program check the week number using the switch case and display the corresponding day. Like the above program also I assumed that Monday is the first day of the week.

Java

1

import java.util.Scanner;

2

public class Weekday{

3

public static void main(String[] args) {

4

Scanner scanner = new Scanner(System.in);

5

6

System.out.println("Enter weekday number : ");

7

int weekday = scanner.nextInt();

8

9

if(weekday == 1) {

10

11

System.out.println("Monday");

12

13

} else if(weekday == 2) {

14

15

System.out.println("Tuesday");

16

17

} else if(weekday == 3) {

18

19

System.out.println("Wednesday");

20

21

} else if(weekday == 4) {

22

23

System.out.println("Thursday");

24

25

} else if(weekday == 5) {

26

27

System.out.println("Friday");

28

29

} else if(weekday == 6) {

30

31

System.out.println("Saturday");

32

33

} else if(weekday == 7) {

34

35

System.out.println("Sunday");

36

37

} else {

38

39

System.out.println("Please enter weekday number between 1-7.");

40

}

41

}

42

}

43

Example 2: Program to display weekday between 1 to 7 using switch-case

The below program used a const string array to store the days on the corresponding array index. Now ask the user to enter the valid week number. After getting the value from the user get the days from the array using this value as an array index and display the day. Like the above program also I assumed that Monday is the first day of the week.

Java

1

import java.util.Scanner;

2

public class Weekday_between_1_to_7 {

3

public static void main(String[] args) {

4

Scanner scanner = new Scanner(System.in);

5

6

System.out.println("Enter weekday number : ");

7

int weekday = scanner.nextInt();

8

9

switch (weekday)

10

{

11

case 1:

12

System.out.println("Monday");

13

break;

14

case 2:

15

System.out.println("Tuesday");

16

break;

17

case 3:

18

System.out.println("Wednesday");

19

break;

20

case 4:

21

System.out.println("Thursday");

22

break;

23

case 5:

24

System.out.println("Friday");

25

break;

26

case 6:

27

System.out.println("Saturday");

28

break;

29

case 7:

30

System.out.println("Sunday");

31

break;

32

default:

33

System.out.println("Please enter weekday number between 1-7.");

34

}

35

}

36

}

37

Recommended:

Java Program to solve quadratic equations

Java Program to find the greatest of three Numbers

Check if the given number is prime or not in Java

Java Program Count invalid Mobile numbers form list

Java program Fill a bucket with water using a mug

Java Program to Check if Number is Positive or Negative

Post navigation

Previous PostNext Post

Leave a Comment

Your email address will not be published. Required fields are marked *

Type here..

Type here..

Name*

Name*

Email*

Email*

Website

Website

Save my name, email, and website in this browser for the next time I comment.

Search for:

Search …

Categories

Categories

Select Category

Recent Posts

Offer to customers in Java program

Sum of integers at even or odd places in Java

Character Addition in Java

Bike Rent Program in Java

Alternate Number differences in Java

Java program to Sum of Even Element

Similar questions