Java Record 1
Java Record 1
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 java.util.Scanner;
public class ArithmaticOperation {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num1;
int num2;
System.out.println("Enter number");
num1=sc.nextInt();
num2=sc.nextInt();
char input;
do {
int choice;
System.out.println("Enter your choice 1 to add or 2 to sub");
choice=sc.nextInt();
switch(choice) {
case 1:
System.out.println(num1+num2);
break;
case 2:
System.out.println(num1-num2);
break;
default:
System.out.println("Invalid");
break;
}
System.out.println("Enter y or Y to continue:");
input=sc.next().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