Using the switch statement, write a program that given the integer input month and integer input year, will output a the sentence replacing the integer month with the string representing the month's name.
Enter month: 5
Enter year: 1978
Output
Your date is May 1978.
Answers
Following are the program in java language is given below
Explanation:
import java.util.*; // import package
public class Main // main class
{
public static void main(String[] args) // main function
{
Scanner sc = new Scanner(System.in); // scanner class
int month,year; // variable declaration
String month1=""; // variable declaration
System.out.println("Enter month:");
month=sc.nextInt(); //Read month by the user
System.out.println("Enter year:");
year=sc.nextInt(); //Read year by the user
switch(month) // switch statement
{
case 1:
month1="January";
break;
case 2:
month1="February";
break;
case 3:
month1="March";
break;
case 4:
month1="April";
break;
case 5:
month1="May";
break;
case 6:
month1="June";
break;
case 7:
month1="July";
break;
case 8:
month1="August";
break;
case 9:
month1="September";
break;
case 10:
month1="October";
break;
case 11:
month1="November";
break;
case 12:
month1="December";
break;
default:
System.out.println("Invalid Month.");
break;
}
System.out.print("Your date is ");
System.out.print( month1 + " "); //Display
System.out.print( year);
}
}
Output:
Enter month: 5
Enter year: 1978
Your date is May 1978
Following are the description of program
- Create a object of scanner class "sc" for the user input
- Read the value of month and year in month and year variable by the user .
- After that used the switch statement to store the value of the respective month in the "month1" variable .
- Finally print the month1 variable .
Learn More :
- brainly.in/question/11395963
Answer:
Explanation:
import java.util.*; // import package
public class Main // main class
{
public static void main(String[] args) // main function
{
Scanner sc = new Scanner(System.in); // scanner class
int month,year; // variable declaration
String month1=""; // variable declaration
System.out.println("Enter month:");
month=sc.nextInt(); //Read month by the user
System.out.println("Enter year:");
year=sc.nextInt(); //Read year by the user
switch(month) // switch statement
{
case 1:
month1="January";
break;
case 2:
month1="February";
break;
case 3:
month1="March";
break;
case 4:
month1="April";
break;
case 5:
month1="May";
break;
case 6:
month1="June";
break;
case 7:
month1="July";
break;
case 8:
month1="August";
break;
case 9:
month1="September";
break;
case 10:
month1="October";
break;
case 11:
month1="November";
break;
case 12:
month1="December";
break;
default:
System.out.println("Invalid Month.");
break;
}
System.out.print("Your date is ");
System.out.print( month1 + " "); //Display
System.out.print( year);
}
}