How to swap values between two variables without using a
third variable?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 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 |
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 |
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 |
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 |
Please describe an example where you used object orientation in one of your programs.
As per interoperatbility programs written in one language can be used by other language. How can we restrict the features of one language (say C#) in the programmer written in another language (say VB)..for example we have some features like operator overloading which is possible C#.NET (not in VB.NET), how can we restrict that when we are using this code in VB.NET.
What is the difference between WebIntelligence and Designer in creating universes?
what is database?how to tell 3years boy or girl by using toys?
what you mean by mantis in IT interview? why we use mantis? advantage of mantis?
hai i like to do some certifications in programming or any other......... am doing my IT third year......... pls guide me...... mail me ur suggestions to ranjithmaharaj@gmail.com...
How to call dll API sub routine in VB Form.
what is the use of check point
What is Negative testing?
Data structure used to impliment a menu:
can u help me? how do solve the transpose problem in ireport? if any one know send to eswaran_2006@yahoo.co.in
How Many versions are there in crystal reports