DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING
Ex no: 1
IMPLEMENTATION OF CONTROL STRUCTURES
Date:
Question
1. Write a program that displays a menu with options 1. Add 2. Sub Based
on the options chosen, read 2 numbers and perform the relevant
operation. After performing the operation, the program should ask the
user if he wants to continue. If the user presses y or Y, then the program
should continue displaying the menu else the program should
terminate.
Aim
To Write a program to perform add and sub using switch case and scanner class.
Code
import [Link];
public class ArithmaticOperation {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int num1;
int num2;
[Link]("Enter number");
num1=[Link]();
num2=[Link]();
char input;
do {
int choice;
[Link]("Enter your choice 1 to add or 2 to sub");
choice=[Link]();
switch(choice) {
case 1:
[Link](num1+num2);
break;
case 2:
[Link](num1-num2);
break;
default:
[Link]("Invalid");
break;
}
[Link]("Enter y or Y to continue:");
input=[Link]().charAt(0);
}while(input=='y'||input=='Y');
}
}
1 717823P113
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING
Output
Result
Thus, the Java program for the given relationship has been successfully developed and the
output was verified.
2 717823P113