How to swap variables values?
Swapping is to change values of each other. e.g I have two variables int a = 5; int b = 7; I want to swap both values like a = 7l and b = 5 1. Using third variable - We can swap values taking third variable. e.g int a = 5.; int b = 7; int temp; //Assign value to temp variable temp = a; //Change the value of a to b a = b; //Last set value of b b = temp; So now output is a = 7 and b = 5 2. Using addition and Subtraction method - int a = 5; int b = 7; //Make addition of both numbers a = a + b; //To find out value of b b = a - b; //To find the value of a by new b value a = a - b; 3. Using Multiplication and Division method - int a = 5; int b = 7; //Make addition of both numbers a = a * b; //To find out value of b b = a / b; //To find the value of a by new b value a = a / b; |
Comments
Post a Comment