Dipa MehtaRank #361 · 6.9K reputationPosted Nov 25, 2019, 12:50 AM EST
+1
import java.util.Scanner;
public class Calculate
{
public static void main(String[] args)
{
int m, n, opt, add, sub, mul;
double div;
Scanner s = new Scanner(System.in);
System.out.print("Enter first number:");
m = s.nextInt();
System.out.print("Enter second number:");
n = s.nextInt();
while(true)
{
System.out.println("Enter 1 for addition");
System.out.println("Enter 2 for subtraction");
System.out.println("Enter 3 for multiplication");
System.out.println("Enter 4 for division");
System.out.println("Enter 5 to Exit");
opt = s.nextInt();
switch(opt)
{
case 1:
add = m + n;
System.out.println("Result:"+add);
break;
case 2:
sub = m - n;
System.out.println("Result:"+sub);
break;
case 3:
mul = m * n;
System.out.println("Result:"+mul);
break;
case 4:
div = (double)m / n;
System.out.println("Result:"+div);
break;
case 5:
System.exit(0);
}
}
}
}
Try this code.
Thanks
Hey Ifeoluwa Omobolanle, you can make the program for addition, subtraction, and multiplication of two numbers using different ways in Java. I am giving you the solution using switch...case:
Madan ShekarRank #428 · 5.9K reputationPosted Apr 19, 2019, 4:02 AM EDT
IF IT HELPS MARKS AS ANSWER
Dipa MehtaRank #361 · 6.9K reputationPosted Nov 25, 2019, 12:50 AM EST
Harikrishna Kundariya162 reputationPosted May 29, 2019, 2:14 AM EDT
Vijay Patel15 reputationPosted Jul 1, 2020, 9:00 PM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts