Answer to Question #295652 in Java | JSP | JSF for Khizee

Question #295652

Create a simple calculator application in Java which will perform four basic



arithmetic operations (plus, minus, multiplication and division) on a pair of



numbers (input from user) from 0 – 9, except division by zero. It means, this



program will perform arithmetic operations between single digits only, but the



result can be more than one digit and in decimals, obviously. Sample output is



given below:



Enter first number between 0 and 9: 6



Enter second number between 0 and 9: 4



Applying arithmetic operations:



Adding 6 and 4 results = 10



Subtracting 4 from 6 results = 2



Multiplying 6 with 4 results = 24



Dividing 6 by 4 results = 1.5

1
Expert's answer
2022-02-09T13:08:09-0500


import java.util.Scanner;


class App {


	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in);
		System.out.print("Enter first number between 0 and 9: ");
		int n1 = keyBoard.nextInt();
		System.out.print("Enter second number between 0 and 9: ");
		int n2 = keyBoard.nextInt();


		System.out.println("Applying arithmetic operations:");
		int result = n1 + n2;
		System.out.printf("Adding %d and %d results =  %d\n", n1, n2, result);
		result = n1 - n2;
		System.out.printf("Subtracting %d from %d results =  %d\n", n1, n2, result);
		result = n1 * n2;
		System.out.printf("Multiplying %d with %d results =  %d\n", n1, n2, result);
		double resultDiv = (double) n1 / (double) n2;
		System.out.printf("Dividing %d by %d results =  %.1f\n", n1, n2, resultDiv);
		keyBoard.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS