Calculator with user input (Using scaner)
Class = Cal
import java.util.Scanner;
public class Cal {
public static void main(String[] args) {
temp();
}
public static void oparator(int x, int y,String oparator) {
if("ADD".equals(oparator.toUpperCase())){
int z = x + y;
System.out.println(z);
}
else if("SUB".equals(oparator.toUpperCase())) {
int h = x - y;
System.out.println(h);
}
else if("DIV".equals(oparator.toUpperCase())) {
int i = x / y;
System.out.println(i);
}
else if("REM".equals(oparator.toUpperCase())) {
int j = x % y;
System.out.println(j);
}
else if("MUL".equals(oparator.toUpperCase())) {
int k = x * y;
System.out.println(k);
}
}
public static void temp(){
Scanner sc=new Scanner(System.in);
System.out.println("Seclect the Oparator");
String oparator=sc.nextLine();
System.out.println("Enter a first number: ");
int FirstNumber=sc.nextInt();
System.out.println("Enter a Second number: ");
int secondNumber=sc.nextInt();
oparator(FirstNumber,secondNumber,oparator);
temp();
}
}
You can add more operators
Operator
|
Description
|
+ (Addition)
|
Adds
values on either side of the operator.
|
- (Subtraction)
|
Subtracts
right-hand operand from left-hand operand.
|
* (Multiplication)
|
Multiplies
values on either side of the operator.
|
/ (Division)
|
Divides
left-hand operand by right-hand operand.
|
% (Modulus)
|
Divides
left-hand operand by right-hand operand and returns remainder.
|
++ (Increment)
|
Increases
the value of operand by 1.
|
-- (Decrement)
|
Decreases
the value of operand by 1.
|
Calculator without user input
public class cal{
public static void main (String args[]){
int x = 5 ;
int y = 6 ;
System.out.println("x + y =" + (x+y));
System.out.println("x - y =" + (x-y));
System.out.println("x / y =" + (x/y));
System.out.println("x * y =" + (x*y));
System.out.println("x % y =" + (x%y));
}
}
Sketch
You can change the data printed in green
public
class cal{
public static void main (String
args[]){
int x = 5 ;
int y = 6 ;
System.out.println("x
+ y =" + (x+y));
System.out.println("x
- y =" + (x-y));
System.out.println("x
/ y =" + (x/y));
System.out.println("x
* y =" + (x*y));
System.out.println("x
% y =" + (x%y));
}
}