Asked by Souad
Write an application that prompts the user for two integers and then prompts the user to enter an option as follows: 1 to add the two integers, 2 to subtract the second integer from the first, 3 to multiply the integers, and 4 to divide the first integer by the second. Display an error message if the user enters an option other than 1 through 4 or if the user chooses the divide option but enters 0 for the second integer. Otherwise, display the results of the arithmetic. Save the file as Calculate.java.
Answers
Answered by
Steve
I assume you can handle the java syntax, but the flow of logic is
integer a,b,c,option,err
print "Enter two integers: "
read a,b
print "Enter option:
1: add
2: subtract
3: multiply
4: divide "
read option
err=0
case option {
1: c=a+b
2: c=a-b
3: c=a*b
4: if (b==0){
err=1
print "Division by zero not allowed"
}else {c=a/b}
else: {err=1;print "Invalid option"}
}
if err==0 print c
integer a,b,c,option,err
print "Enter two integers: "
read a,b
print "Enter option:
1: add
2: subtract
3: multiply
4: divide "
read option
err=0
case option {
1: c=a+b
2: c=a-b
3: c=a*b
4: if (b==0){
err=1
print "Division by zero not allowed"
}else {c=a/b}
else: {err=1;print "Invalid option"}
}
if err==0 print c
There are no AI answers yet. The ability to request AI answers is coming soon!
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.