I have to swap two variables with a number value without using a third variable. What is the simple solution?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Let us see one of the method namely by using arithmetic operators.
Consider 2 variables say x=50 and y=70 and let us see how to swap the value of two variables that is make x=70 and y=50 without using third variable. This can be done by using following arithmetic operations namely
x= x + y
y= x – y
x= x – y
Which gives
• x= x + y gives x= 70 + 50 an so x is equal to 120
• y= x – y gives y = 120 – 70 which makes the value of y as 50
• x= x – y gives x= 120 – 50 and thus value of x becomes 70