-->

Combine to form the number

Posted by Admin on
Given a, b and c, return true if any one of them can be formed by a mathematical operation using the other two numbers. the mathematical operations permitted are addition, subtraction, multiplication and division. For e.g if a=12, b = 15, c = 3 output is true (15-12 = 3).



public class CheckCombination {

       static int testcase11 = 256;
       static int testcase12 = 0;
       static int testcase13 = 512;
      
       public static void main(String args[]){
              CheckCombination testInstance = new CheckCombination();
              boolean result = testInstance.combine(testcase11,testcase12,testcase13);
              System.out.println(result);
       }
      
       //write your code here
       public boolean combine(int a, int b,int c){
       boolean bool=false;
       if(bool=(subtraction(a,b,c))==true)
       {
              return true;
       }
       else if(bool=(multiply(a,b,c))==true)
       {
              return true;
       }
       else if(bool=(addition(a,b,c))==true)
       {
              return true;
       }
       else if(bool=(divide(a,b,c))==true)
       {
              return true;
       }
       else
       {
              return false;
       }
       }

       private boolean subtraction(int a, int b, int c) {
              int res1=a-b;
              int res2=b-c;
              int res3=a-c;
              int res4=b-a;
              int res5=c-a;
              int res6=c-b;
              if(res1==c || res2==a ||res3==b ||res4==c ||res5==b ||res6==a )
              {
              return true;
              }
              else
              {
              return false;
              }
       }
      
      

       private boolean addition(int a, int b, int c) {
              int res1=a+b;
              int res2=b+c;
              int res3=a+c;
              if(res1==c || res2==a ||res3==b )
              {
              return true;
              }
              else {
              return false;
              }
       }

       private boolean multiply(int a, int b, int c) {
              int res1=a*b;
              int res2=b*c;
              int res3=a*c;
              if(res1==c || res2==a ||res3==b )
              {
                     return true;
              }
              else {
              return false;
              }
       }

       private boolean divide(int a, int b, int c) {
              boolean ans=false;;
              if((b!=0) && (c==(a/b))){ 
                     ans=true;
              }
              if((c!=0)&&(b==(a/c))){
                     ans=true;
              }
             
              if((a!=0) &&(c==(b/a))){
                     ans=true;
              }
              if((b!=0) && (a==(c/b))){
                     ans=true;
              }
              if((c!=0) &&(a==(b/c))){
                     ans=true;
              }
               
              if((a!=0)&&(b==(c/a))){
                     ans=true;
              }
             
              return ans;

       }
}


Please comment if you find any mistake


No comments:

Post a Comment