In: Computer Science
Introduction to Java Programing
Using Loop
Please find the below java code:
//import statements
import java.util.*;
import java.lang.*;
//main class
public class ArithmeticOperation {
public static void main(String[] args){
System.out.println("Arithmetic Operations");
Scanner sc=new Scanner(System.in);
System.out.println("please enter first number");
int num1=sc.nextInt();// reading first number
System.out.println("please enter second
number");
int num2=sc.nextInt();// reading second number
int select=1;
while(select!=0)// condition for
quit
{
int
choice=selectoperation();//calling function for options and
returning the selection
select=choice;
performoperation(choice,num1,num2);//calling function for
performing arithmetic calculation and priting the result
}
}
static int selectoperation(){
System.out.println("Please select
one of the options below ");
System.out.println("0.Quit \n1.Add
\n2.Substract \n3.Mutiply \n4.Divide");
Scanner sc=new
Scanner(System.in);
int c=sc.nextInt();
return c;
}
static void performoperation(int d,int num1,int
num2){
switch(d){
case 1 :
System.out.println("Selected option is Addition");
System.out.println("Addition:"+(num1+num2));
break;
case 2 :
System.out.println("Selected option is Substraction");
System.out.println("Substraction:"+(num1-num2));
break;
case 3 :
System.out.println("Selected option is Multiplication");
System.out.println("Multiplication:"+(num1*num2));
break;
case 4 :
System.out.println("Selected option is Division");
float
div=(float)num1/(float)num2;
System.out.println("Division:"+div);
break;
case 0 :
System.out.println("Selected option is Quit");
System.out.println("Quitting");
break;
default :
System.out.println("Invalid selection");
}
}
}