swapping

swapping

How to swap values of two variable without using third variable.

Eg: if a=10 b=20

after swap a=20 b=10

View Answers

May 21, 2010 at 12:04 PM

Hi Friend,

Try the following code:

class Swapping {
public static void main(String[] args) {
int num1 = 10;
int num2 = 20;
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
System.out.println("After swapping, num1= " + num1 + " and num2= "+ num2);
}
}

Thanks









Related Tutorials/Questions & Answers:
swapping - Java Beginners
swapping   How to swap values of two variable without using third..., Try the following code: class Swapping { public static void main(String... = num1 - num2; num1 = num1 - num2; System.out.println("After swapping, num1
swapping - Java Beginners
;Hi Friend, Try the following code: import java.util.*; class Swapping...; num2=num1-num2; num1=num1-num2; System.out.println("After swapping
Advertisements
swapping - Java Interview Questions
swapping  if input is 123456 then output should be 214365. how to do it using java code?  Hi friend, As your requirement Code to swapping : import java.io.*; public class SwapNumbers { public static void
Dynamically Swapping Images with JavaScript
Dynamically Swapping Images with JavaScript  ... the numbers Swapping in JavaScript and create a  JavaScript program. Image Swapping in JavaScript:- In JavaScript, Numbers Swapping means interchange
Swapping of two numbers
Swapping of two numbers      ... program to calculate swap of two numbers. Swapping is used where  you want... swap the numbers. To swap two numbers first we have to declare a class Swapping
Swapping of two numbers in java
Swapping of two numbers in java In this example we are going to describe swapping of two numbers in java  without using the third number in java. We... values from the command prompt. The swapping of two numbers is based on simple
Swapping of two numbers without using third variable
Swapping of two numbers without using third variable In this tutorial we will learn about swapping of two number in java without using third variable. This is simple program to swap two value using arithmetic operation Swapping of two
Bubble Sort in Java
is sorted. Swapping is followed carried out repeatedly till the list is sorted. Bubble..., it goes back to first pair. The process of comparing, swapping and returning to first
Quick Sort in Java
JAVA PROGRAMMING
JAVA PROGRAMMING  Write an OVERLOADED FUNCTION in java that swaps(interchanges) 2 user given numbers.(one integer and one float value) and displays the result after swapping. Invoke the function in a class
xor operator not working correctly with array
xor operator not working correctly with array  main { int a[2] = {2,3}; printf("before swapping: %d %d\n",a[0],a[1]); //a[0] ^= a[1]; //a[1...] ^= a[1]; //this didn't work correctly why? printf("after swapping: %d %d\n
xor operator not working correctly with array
xor operator not working correctly with array  main { int a[2] = {2,3}; printf("before swapping: %d %d\n",a[0],a[1]); //a[0] ^= a[1]; //a[1...] ^= a[1]; //this didn't work correctly why? printf("after swapping: %d %d\n
PHP Call By Reference
_TO_REPLACE_3 $var1=34; $var2=90; echo "<b>Before swapping..._TO_REPLACE_4 swap($var1,$var2); echo "<b>After swapping values...;ADS_TO_REPLACE_5 Output: Before swapping values $var1= 34 & $var2= 90
Swap two numbers without using third variable
: import java.util.*; class Swapping { public static void main(String[] args...; num1 = num1 - num2; System.out.println("After swapping, num1= " + num1... Number 1: 20 Enter Number 2: 10 After swapping, num1
function arguments in java
code   public class Swapping{ static void swap(int i,int j){ int temp=i; i=j; j=temp; System.out.println("After swapping i = " + i + " j...; System.out.prinln("Before swapping i="+i+" j="+j); swap(i,j
java - Java Beginners
: class Swapping { public static void main(String[] args) { int num1 = 10... - num2; System.out.println("After swapping, num1= " + num1 + " and num2
Program to swap the string
(); System.out.println(); System.out.println("Before swapping...("After swapping:"); System.out.println("st1="+st1+" and st2="+st2
xor operator not working correctly with array
xor operator not working correctly with array  main { int a[2] = {2,3}; printf("before swapping: %d %d\n",a[0],a[1]); //a[0] ^= a[1]; //a[1...] ^= a[1]; //this didn't work correctly why? printf("after swapping: %d %d\n
java - JSP-Servlet
place. thanks!!  Hello Without swapping u can pay money
java
pament in my account through A.T.M. tranjections or swapping machin.could u give
JDBC
selection?   public class Swapping{ static void swap(int i,int j){ int temp=i; i=j; j=temp; System.out.println("After swapping i = " + i + " j...; System.out.prinln("Before swapping i="+i+" j="+j); swap(i,j
confusion
, 1)For swapping, use following code: int num1=5,num2=10; num1 = num1
Core Java Doubts - Java Beginners
; Hi Friend, 1) class Swapping { public static void main(String[] args... - num2; num1 = num1 - num2; System.out.println("After swapping, num1= " + num1
Working with java Collections class
, list.size() - 1); System.out.println("List after swapping : " + list... : 5 List after swapping : [replaced, replaced, girish, vineet, amit] fill all
Remove From Super View - removeFromSuperview
are willing to load it again (in case of swapping views) you required to retain
What is D2D?
sort of tape. D2D including SATA RAID also provides hot swapping. So D2D has
Java Script With Links and Images
that need to be edited to create JavaScript image swapping.  The JavaScript
10 Unusual Technologies to Come in a Big Way
to wearable electronics and internet ready garments to paying bills by swapping..., instead you can just do all your monetary transactions by just swapping your cell... cash just using your cell phone instead of punching or swapping your cards
Odd Even Transposition Sort In Java
is based on the Bubble Sort technique of comparing two numbers and swapping
JavaScript - JavaScript Tutorial
; Dynamically swapping the images with JavaScript In this lesson you
JavaScript - JavaScript Tutorial
; Dynamically swapping the images with JavaScript In this lesson you
Sitemap Java Script Tutorial
Validation with JavaScript | Dynamically swapping the images with JavaScript
What is Android Technology?
movements like tapping, swapping, pinching, finger spreading, all can
Android 4.3 Jelly Bean - What's new in Android 4.3 Jelly Bean?
swapping over the keyboard. With more powerful predictive text output typing
JavaScript - JavaScript Tutorial
swapping the images with JavaScript In this lesson you will learn how
EJB, Enterprise java bean, EJB Intoduction- Enterprise Java Beans (EJB) - An Introduction
such as instance swapping, resource pooling, and activation may
Beginners Java Tutorial
as static static.    Swapping of two numbers This Java... swap of two numbers. Swapping is used where  you want to interchange

Ads