Saturday, 7 May 2022

Swap two variables in Java

 


  • With temp variable
                public void swap(int a,int b)
                    {
                        int temp=a;
                        a=b;
                        b=temp;
                    }


  • Without temp variable
                public void swap(int a,int b)
                    {
                        a=a+b;
                        b=a-b;
                        a=a-b;
                    }

No comments:

Post a Comment

Switch case in Java

 Problem statement: Return the capital of a state based on input state          public String getCapital(String state){               switch...