Java Exception Handling | Practice | GeeksforGeeks

Given two integers (positive , negative or even 0) say a and b. Find the minimum value of a$b where $ is any arithmetic operation like multiply(*), Divide(/), Addition(+),Substraction(-). Use Exception Handling in this problem.
Input: 
The first line will contain an integer T (number of test cases). Each test case will contain two integers a and b.
Output: 
Print the minimum value of a$b.
Constraints: 
1 < = T < = 100
-103<=a,b<=103
Example:
 Input:
2
5 -5
4 0
Output:
-25
0
EXPLANATION:
In test case 1: 5*(-5) = -25
In test case 2: 4*0 = 0
0
shakirmalik707861 week ago
   // Add your code here. Define private variables, Constructors to initialize    // variables with the given values and a method findMin() which prints the    // Minimum value of a$b.   private int a,b;   public TestClass(int a, int b) {       this.a = a;       this.b = b;   }      public void findMin() {       try{           int []arr = new int[4];           arr[0] = a + b;           arr[1] = a - b;           arr[2] = a * b;           arr[3] = a / b;           int min = Math.min( Math.min(arr[0], arr[1]), Math.min(arr[2], arr[3]));           System.out.println(min);       }       catch (Exception e) {           System.out.println(0);       }   }   
0
anas0shah03 weeks ago
class TestClass{   // Add your code here. Define private variables, Constructors to initialize    // variables with the given values and a method findMin() which prints the    private nt a;   private int b;      // Minimum value of a$b.   TestClass(int a, int b){       this.a = a;       this.b= b;          }   void findMin(){            try{      int sum=a+b;      int diff=a-b;      int mul=a*b;      int div=a/b;            int x = Math.min(Math.min(sum,diff),Math.min(mul,div));                  System.out.println(x);            }catch(Exception e)      {      System.out.println(0);      }          }   }
0
debasishtewary51 month ago
 private int a;private int b;
public TestClass(int a, int b) { // TODO Auto-generated constructor stub    this.a=a;    this.b=b;}   public void findMin(){       int min=Math.min((a+b),(a-b));       int min1=Math.min((a*b),min);       try{          int c= a/b;          System.out.println(Math.min(c,min1));       }       catch(Exception e){           System.out.println(min1);       }   }
+1
exponential6 months ago
private int a,b;      public TestClass(int a,int b)   {       this.a=a;       this.b=b;   }      public void findMin()   {         if(b==0)       {           System.out.println(a*b);       }       try{       int sum=a+b;       int diff=a-b;       int mul=a*b;       int div=a/b;              int x = Math.min(Math.min(sum,diff),Math.min(mul,div));                     System.out.println(x);              }catch(Exception e)       {              }   }
0
Karandeep Singh8 months ago
Karandeep Singh
class TestClass{    // Add your code here. Define private variables, Constructors to initialize     // variables with the given values and a method findMin() which prints the     // Minimum value of a$b.    int a;    int b;    TestClass(int p, int q) //constructor    {    a = p;    b = q;    }    public void findMin() {        int arr[] = new int[4];        arr[0] = a*b;        arr[1] = a-b;        arr[2] = a+b;        try        {        arr[3]= a/b;        }        catch(Exception e)        { System.out.print("");         }        int min = arr[0];        for(int i=0; i<4;i++){            if(min>arr[i])                min = arr[i];        }        System.out.println(min);    }}
0
Amir Ansari1 year ago
Amir Ansari
Correct AnswerExecution Time:0.21
class TestClass{    // Add your code here. Define private variables, Constructors to initialize     // variables with the given values and a method findMin() which prints the     // Minimum value of a$b.    private int a;    private int b;
     TestClass(int a, int b){         this.a = a;         this.b = b;     }     public void findMin(){
         int [] numArr = new int[4];         numArr[0] = a*b;         numArr[1] = a-b;         numArr[2] = a+b;
         try{             numArr[3] = a/b;         } catch(Exception e){             System.out.print("");         }          int min = Integer.MAX_VALUE;
          for(int i = 0; i < 4;i++){              min = Math.min(min,numArr[i]);          }           System.out.println(min);
     }
}
0
Arunoday kumar2 years ago
Arunoday kumar
class TestClass{    int a;    int b;
    TestClass(int x,int y){        a = x;        b = y;    }    public void findMin(){        int c[] = new int[4];         c[0] = (a*b);
        c[1] = a+b;         c[2] = a-b;        try{        c[3] = a/b;        }        catch(Exception e){            System.out.print("");        }
        int min = c[0];        for(int i= 0;i<4;i++){            if(min>c[i])            min = c[i];        }
        System.out.println(min);
    }// Add your code here. Define private variables, Constructors to initialize     // variables with the given values and a method findMin() which prints the     // Minimum value of a$b.}
0
Sameer Anand2 years ago
Sameer Anand
int a;      int b;    TestClass(int p, int q) //constructor    {         a = p;         b = q;    }    public void findMin()    {    int arr[] = new int[4];     arr[0] = a*b;     arr[1] = a-b;     arr[2] =  a+b;
    try    {         arr[3]= a/b;    }    catch(Exception e)    {   System.out.print("");
    }     int m = arr[0];     for (int i = 0; i < 4; i++)     if(arr[i]<m) m="arr[i];" system.out.println(m);="" }="" }="">
0
Hitesh Yadav3 years ago
Hitesh Yadav
lass TestClass{    // Add your code here. Define private variables, Constructors to initialize     // variables with the given values and a method findMin() which prints the     // Minimum value of a$b.
    int a,b,result=0,min=0;    TestClass(int x, int y){        this.a=x;        this.b=y;    }    void findMin(){    try{
        result=a*b;        min=result;
        result=a/b;        if(result<min) min="result;" result="a+b;" if(result<min)="" min="result;" result="a-b;" if(result<min)="" min="result;" }="" catch="" (arithmeticexception="" e="" ){}="" system.out.println(min);="" }="" }="">
0
Shubham Sharma3 years ago
Shubham Sharma
class TestClass{    int x,y;    TestClass(int a,int b){        x=a;        y=b;    }    public void findMin(){        int min;        min=x+y;        if((x*y)<min) {="" min="x*y;" }="" if((x-y)<min)="" {="" min="x-y;" }="" try{="" if((x="" y)<min){="" min="x/y;" }="" }="" catch(arithmeticexception="" e){="" system.out.print("");="" }="" system.out.println(min);="" }="" }="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.