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
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();
}
}
Comments
Leave a comment