How to swap values between two variables without using a
third variable?

Answers were Sorted based on User's Feedback



How to swap values between two variables without using a third variable?..

Answer / pinky

main()
{
int ia,b;

a=a+b;
b=a-b;
a=a-b;

Is This Answer Correct ?    120 Yes 24 No

How to swap values between two variables without using a third variable?..

Answer / banti sharma

main()
{
int a,b;

a=a+b;
b=a-b;
a=a-b;
}
in Answer of pinky she has taken a variale ia which is not
used in program. He He He

Is This Answer Correct ?    88 Yes 16 No

How to swap values between two variables without using a third variable?..

Answer / shobha

int x=10,y=20;
x=x+y (10+20=30)
y=x-y (30-20=10)
x=x-y (30-10=20)

x=20,y=10
x and y vales are swaped.

Is This Answer Correct ?    51 Yes 12 No

How to swap values between two variables without using a third variable?..

Answer / mona

swap(int var1,int var2)
{
var1=var1+var2;
var2=var1-var2;
var1=var1-var2;
}

thanks for asking.

Is This Answer Correct ?    45 Yes 13 No

How to swap values between two variables without using a third variable?..

Answer / siva

You can also do it with the exclusive or bitwise operator...
a = a ^ b;
b = a ^ b; ((a ^ b) ^ b == a)
a = a ^ b; ((a ^ b) ^ a == b)

Is This Answer Correct ?    27 Yes 8 No

How to swap values between two variables without using a third variable?..

Answer / gourab varma

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter value of a and b");
scanf("%d%d", &a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("the value of a=%d\n the value of b is=%d", a, b);
getch();
}

Is This Answer Correct ?    8 Yes 1 No

How to swap values between two variables without using a third variable?..

Answer / shyam sundar baishya

How to swap two String values without using a
third variable?

In Java::
=========
String s1 = "Shyam Sundar Baishya" ;
String s2 ="Dilip Kumar";
System.out.println("Before swapping ............");
System.out.println("s3 == "+s1);
System.out.println("s4 == "+s2);
s1= s1+s2;
System.out.println("....................");
System.out.println("After swapping ............");
s2 = s1.substring(0,(s1.length()-s2.length()));
s1 = s1.substring(s2.length(),(s1.length()));
System.out.println("s3 == "+ s1);
System.out.println("s4 == "+ s2);

Is This Answer Correct ?    6 Yes 0 No

How to swap values between two variables without using a third variable?..

Answer / pushpendra

int x=10,y=20;
x=x+y (10+20=30)
y=x-y (30-20=10)
x=x-y (30-10=20)

x=20,y=10
x and y vales are swaped.

Is This Answer Correct ?    13 Yes 9 No

How to swap values between two variables without using a third variable?..

Answer / sriram

Hey guys, what about swaping variables contains string
values? All of your solutions will suck... Try this and swap
any datatype without third variable... Happy Sensible Coding..

$v = 'sriram';
$u = 'lakshmi';

$v .= $u;
$u = substr($v,0,(strlen($v) - strlen($u)));
$v = substr($v,(strlen($v) - strlen($u)-1), strlen($v));

echo 'u = ' . $u .'<br>';
echo 'v = ' . $v;

Is This Answer Correct ?    8 Yes 4 No

How to swap values between two variables without using a third variable?..

Answer / vinodkumar

main()
{
int a=10,b=8;
void swap(a,b)
/*it does not return any value because return statement can
return only one value at a time*/
{
a=a+b;/*a=10+8,a=18*/
b=a-b;/*b=18-8,b=10*/
a=a-b;/*a=18-10,a=8*/
printf("after swaping a= %dand b=%d",&a,&b);
}
getch()
}

Is This Answer Correct ?    5 Yes 2 No

Post New Answer

More Programming Languages AllOther Interview Questions

how CLR identify vb file?

0 Answers   Tech Mahindra,


what is the difference between a namespace and assembly ?

4 Answers   Innominds, Polaris,


What's the difference b/w Table & Templete in Smartform?

0 Answers   Accenture,


what is integration testing in real time applications?

0 Answers   CTS,


You are given a dictionary of all valid words. You have the following 3 operations permitted on a word: delete a character, insert a character, replace a character. Now given two words - word1 and word2 - find the minimum number of steps required to convert word1 to word2. (one operation counts as 1 step.)

0 Answers   Amazon,


3.Give the formula for determine the range of the data type?

1 Answers  


Write a Pseudo Code to fins the LCM of two given numbers

5 Answers   Goldman Sachs,


what is the main usage of an abstract keyword?please follow the program class A { void display() { System.out.println("hai"); } void print() { } } class B extends A { void print() { System.out.println("Hello"); } } In this program i was gives the implementation of print() according to my requirements in subclass.And there is no definition in superclass then why we can use abstract keyword before a method that i want to gives definition in other classes,is any mistakes in the above usage of method?

0 Answers  


why we use static with only main()class not with other class

2 Answers   TCS,


What is meant by STACK?

5 Answers   Mascot,


what is delimiter in sas ?

0 Answers  


write algo for cobol program whichuse three flat file to extract some specific information 8 marks mainframe

0 Answers  


Categories