Untitled
unknown
plain_text
a year ago
1.9 kB
1
Indexable
Never
import java.util.Scanner; public class Operations { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("Enter first integer between 0 to 50 inclusively"); int n1=input.nextInt(); while(true) { if(n1>=0 && n1<=50) break; System.out.println("Error: the entered number is out of range"); System.out.println("Enter first integer between 0 to 50 inclusively"); n1=input.nextInt(); } System.out.println("Enter second integer between -5 to 20 inclusively"); int n2=input.nextInt(); while(true) { if(n2>=-5 && n2<=20) break; System.out.println("Error: the entered number is out of range"); System.out.println("Enter second integer between -5 to 20 inclusively"); n2=input.nextInt(); } System.out.println("Enter operation you want to perform: addition (+), subtraction (-), multiplication (*), division (/), or modulus (%)"); char op=input.next().charAt(0); int result=0; if(op=='+') { result=n1+n2; System.out.printf(String.format("Addition: %d %c %d = %d\n",n1,op,n2,result)); } else if(op=='-') { result=n1-n2; System.out.printf(String.format("Subtraction: %d %c %d = %d\n",n1,op,n2,result)); } else if(op=='*') { result=n1*n2; System.out.printf(String.format("Multiplication: %d %c %d = %d\n",n1,op,n2,result)); } else if(op=='/') { if(n2==0) { System.out.println("Error: Division by zero"); System.exit(0); } result=n1/n2; System.out.printf(String.format("Division: %d %c %d = %d\n",n1,op,n2,result)); } else if(op=='%') { result=n1%n2; System.out.println("Modulus: "+n1+" "+op+" "+n2+"= "+result); } else { System.out.println("Error: Invalid operation"); System.exit(0); } if(result==0) System.out.print("The result is zero"); else if(result>0) System.out.print("The result is positive"); else System.out.println("The result is negative"); } }