Question:
Code for swapping of two numbers without using temporary variable using C.
Answer:
int a=10,b=5;
a=a+b; // a=15 b=a-b; // b=10 a=a-b; // a=5 Source: CoolInterview.com
Answered by: Mohan | Date: 11/27/2007
| Contact Mohan
main() { int a,b; printf("enter the values of a,b"); scanf("%d%d",&a,&b); a=a+b; b=a-b; a=a-b; printf("after swapping the values of a and b"); } Source: CoolInterview.com
Answered by: sukavasi suresh | Date: 11/27/2007
| Contact sukavasi suresh
#include<stdio.h> #include<conio.h> void main() { int a,b;
printf("enter a , b"); scanf("%d%d",&a,&b); printf("a: %d b: %d ",a,b); a=a+b; b=a-b; a=a-b; printf("a: %d",a); printf("b: %d",b); getch(); } Source: CoolInterview.com
Answered by: sourav | Date: 11/27/2007
| Contact sourav
main() { int a,b; printf("Enter values for a & b"); scanf("%d""%d",@a,@b); if(a==b) printf("enter valid numbers"); else { a=a+b; a=a*b; b=a-b; /*(or)*/ b=a/b; a=a-b; a=a/b; } printf("a=%d b=%d",&a,&b); } Source: CoolInterview.com
Answered by: tirumalarao peddineni | Date: 11/28/2007
| Contact tirumalarao peddineni
By using this simple arithmatic you can solve this problem. ====== a=10; b=20;
a=a+b b=a-b a=a-b ====== Source: CoolInterview.com
Answered by: Ashish | Date: 11/29/2007
| Contact Ashish
int a=10, b=20; a=a+b; b=a-b; a=a-b; Source: CoolInterview.com
Answered by: Ashish | Date: 11/29/2007
| Contact Ashish
code is : b=a+b; a=b-a; b=b-a; Source: CoolInterview.com
Answered by: murali | Date: 11/29/2007
| Contact murali
a,b a=a+b b=a-b a=a-b now a and b are swapped Source: CoolInterview.com
Answered by: girish | Date: 11/29/2007
| Contact girish
int i=9,j=8; i=i+j; j=i-j; i=i-j; printf("J = ",&j); System.out.println("I =",&i); Source: CoolInterview.com
Answered by: bablu | Date: 11/29/2007
| Contact bablu
int a = 10; int b = 20;
b = a + b; a = b - a; b = b - a; printf ("a = %d, b = %d ", a, b); Source: CoolInterview.com
Answered by: Nathan | Date: 11/30/2007
| Contact Nathan
a=a+b; b=a-b; a=a-b; Source: CoolInterview.com
Answered by: topcoder | Date: 12/2/2007
| Contact topcoder
void main() {int a=50,b=100; a=a+b; //a=50+100, a= 150 b=a-b; //b=150-100, b= 50 a=a-b; //a=150-50, a=100 } Source: CoolInterview.com
Answered by: Sarwat | Date: 12/2/2007
| Contact Sarwat
swapping of two numbers without using temp veriable
int a,b; a=a+b; b=a-b; a=a-b; now print a and b with swapped values Source: CoolInterview.com
Answered by: SUKHCHAIN SANDHU | Date: 12/3/2007
| Contact SUKHCHAIN SANDHU
#include <stdio.h> void main() { int a,b; printf(" Enter the two number ::"); scanf("%d %d",&a,&b); /*Swapping*/ a=a+b; b=a-b; a=a-b; printf("The swapped numbers are::%d %d",a,b); } Source: CoolInterview.com
Answered by: Jai | Date: 12/3/2007
| Contact Jai
one method :
a=a+b; b=a-b; a=a-b;
other method : a^=b^=a; Source: CoolInterview.com
Answered by: vikas kr | Date: 12/4/2007
| Contact vikas kr
a=a-b; b=a+b; a=b-a; Source: CoolInterview.com
Answered by: Vikas | Date: 12/4/2007
| Contact Vikas
#include<stdio.h>
int main() { int a,b; printf(" Enter the values for a and b"); scanf("%d%d",&a,&b); printf(" Value of a and b before swapping are :: a = %d b = %d
",a,b); a=a+b; b=a-b; a=a-b; printf(" Value of a and b after swapping are :: a = %d b = %d
",a,b); system("pause"); return 0; } Source: CoolInterview.com
Answered by: Syed Baseer Ahmed | Date: 12/4/2007
| Contact Syed Baseer Ahmed
#include<stdio.h>
int main() { int a,b; printf("Enter the two number a and b "); scanf("%d%d", &a,&b); printf("Number before swapping a = %d b = %d", a,b); a = a + b; b = a - b; a = a - b; printf(" Number after swapping a = %d b = %d", a,b); Source: CoolInterview.com
Answered by: praveen.c | Date: 12/4/2007
| Contact praveen.c
#include<stdio.h>
int main() { int a,b; printf("Enter the two number a and b "); scanf("%d%d", &a,&b); printf("Number before swapping a = %d b = %d", a,b); a = a + b; b = a - b; a = a - b; printf(" Number after swapping a = %d b = %d", a,b); return 0; } Source: CoolInterview.com
Answered by: praveen.c | Date: 12/4/2007
| Contact praveen.c
1st method : a = a + b; b = a - b; a = a - b;
2nd method : a = a xor b; b = b xor a; a = a xor b; Source: CoolInterview.com
Answered by: Satish Aradhya | Date: 12/5/2007
| Contact Satish Aradhya
a,b are two variables... a=a+b; b=a-b; a=a-b; Source: CoolInterview.com
Answered by: Naga Bhushanam | Date: 12/5/2007
| Contact Naga Bhushanam
main() { int a,b; printf("Enter values of a,b"); scanf("%d%d",&a,&b);//forex:u enter 100,50 a=a+b;//a=150 b=a-b;//b=100 a=a-b;//a=50 printf("a=%d b=%d",a,b); } Source: CoolInterview.com
Answered by: uppala mamatha | Date: 12/5/2007
| Contact uppala mamatha
#include<stdio.h> #include<conio.h> #include<math.h> void main() { int a,b; printf(" Enter two values"); scanf("%d%d",&a,&b); a=a-b; b=abs(a+b); a=abs(a-b); printf(" Swaped values are %d,%d",a,b); getch(); } Source: CoolInterview.com
Answered by: Jeevan K.M. | Date: 12/6/2007
| Contact Jeevan K.M.
#include<stdio.h> void main() { int a=5,b=7; a=a+b; b=a-b; a=a-b; printf("%d %d",a,b); } output: 7 5 Source: CoolInterview.com
Answered by: sunanda | Date: 12/6/2007
| Contact sunanda
a=a+b; b=a-b; a=a-b; Source: CoolInterview.com
Answered by: vijay jain | Date: 12/6/2007
| Contact vijay jain
If a and b are two variables then swapping can be done in a single statement without using any temporary variable.
a^=b^=a^=b; Source: CoolInterview.com
Answered by: Manoj Kumar | Date: 12/7/2007
| Contact Manoj Kumar
main() { int x,y; temp z; x = y; y = z; z = x; printf("%d%d",x,y); } Source: CoolInterview.com
Answered by: D.N.Pani | Date: 12/8/2007
| Contact D.N.Pani
void main() { int x=5, y=10; swap (x,y); printf(“%d %d ”,x,y); swap2(x,y); printf(“%d %d ”,x,y); } Source: CoolInterview.com
Answered by: lokesh | Date: 12/9/2007
| Contact lokesh
#define swap(a,b) a=a+b;b=a-b;a=a-b;
void main() { int x=5, y=10; swap (x,y); printf(“%d %d ”,x,y); swap2(x,y); printf(“%d %d ”,x,y); }
int swap2(int a, int b) { int temp; temp=a; b=a; a=temp; return 0;
} Source: CoolInterview.com
Answered by: lokesh | Date: 12/10/2007
| Contact lokesh
use, 2 variables a, b.then try this code,
a=a+b; b=a-b; a=a-b;
that's it Source: CoolInterview.com
Answered by: nethaji | Date: 12/10/2007
| Contact nethaji
#include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf(" Enter 2 no: "); scanf("%d%d",&a,&b); printf(" Before swapping a=%d b=%d",a,b); a=(a+b)-(b=a); printf(" After swapping a=%d b=%d",a,b); getch(); }
/*this program is executed in turboc3 */ Source: CoolInterview.com
Answered by: sudarsan dash | Date: 12/10/2007
| Contact sudarsan dash
Using XOR operation will also Solve the purpose a=7, b=10,
a = a^b;//a=13 b = a^b;//b=7 a = a^b;//a=10
a and b are swapped Source: CoolInterview.com
Answered by: Sagar | Date: 12/18/2007
| Contact Sagar
int a,b; a=a^b; b=a^b; a=a^b; Source: CoolInterview.com
Answered by: vish | Date: 12/23/2007
| Contact vish
#include<stdio.h> #include<conio.h>
main() { int a=5,b=7;
a=a^b; b=a^b; a=a^b; printf("a is %d b is %d ",a,b); getch(); return 0; } Source: CoolInterview.com
Answered by: Essam Ali | Date: 1/3/2008
| Contact Essam Ali
void main() { int a=5,a=10; printf(" A=%d B=%d",a,b); a=a-(b=(a=a+b)-b); printf(" A=%d B=%d",a,b); } Source: CoolInterview.com
Answered by: Ram | Date: 2/1/2008
| Contact Ram
This snippet of code will swap 2 numbers a = a + b - (b = a) ; also we can use ^ operator a^b;b^a;a^b; Source: CoolInterview.com
Answered by: bharath | Date: 2/6/2008
| Contact bharath
If a and b are two variables then we can swapped with this code a=a*b/(b=a) Source: CoolInterview.com
Answered by: DHEERAJ SHARMA | Date: 3/11/2008
| Contact DHEERAJ SHARMA
/*using pointers*/ main() { int *a=10, *b=4; printf("BEFORE SWAPPING THE VALUES OF A & B ARE %d%d"*a,*b); *a=*a+*b; //*a=14 *b=*a-*b; //*b=10; *a=*a-*b; //*a=4 printf("AFTER SWAPPING THE VALUES OF A & BARE %d%d"*a,*b); }
Source: CoolInterview.com
Answered by: venkatarathnam.m.v | Date: 3/15/2008
| Contact venkatarathnam.m.v
#include<stdio.h> #include<conio.h> void main() { int a=3,b=2; clrscr(); printf("The values of a and b before swapping are %d%d",a,b); a^=b^=a^=b; printf("The values of a and b after swapping are %d%d",a,b); getch(); } Source: CoolInterview.com
Answered by: mahila | Date: 7/11/2008
| Contact mahila
main() { int a=5,b=10; printf("before swap %d %d",a,b); a=(a*b)/(b=a); printf("after swap %d %d",a,b); Source: CoolInterview.com
Answered by: ravindrabonthu | Date: 10/7/2008
| Contact ravindrabonthu
#include<stdio.h> void main() { int a,b; printf("%d%d",a,b); a=a^b; b=a^b; a=a^b; printf("a=%d,b=%d",a,b); } Source: CoolInterview.com
Answered by: Praveen | Date: 12/17/2008
| Contact Praveen
a=10; b=20 b=((a+b)-(a=b))//in a single line Source: CoolInterview.com
Answered by: Debasis Nayak | Date: 12/28/2008
| Contact Debasis Nayak
#include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("Enter a and b"); scanf("%d%d",&a,&b); a=a*b; b= a/b; a = a/b; printf("Swapped values are:%d,%d",a,b); getch(); } Source: CoolInterview.com
Answered by: GAUATM DESHPANDE. | Date: 7/28/2009
| Contact GAUATM DESHPANDE.
#include<stdio.h> main() { int a,b,i; clrscr(); printf("enter the value of a and b"); scanf("%d %d",&a,&b); a=a+b; printf("a=%d,b=%d",a=a-b,b=a-b); } Source: CoolInterview.com
Answered by: Govind | Date: 8/22/2009
| Contact Govind
main() { int a=6,b=3; a=a^b; b=a^b; a=a^b; printf("%d %d",a,b); } output: 3 6 othermethod a=a+b; b=a-b; a=a-b; Source: CoolInterview.com
Answered by: sushma | Date: 12/9/2009
| Contact sushma
#include<iostrem.h> #include<conio.h> void main() { int a,b,t; printf("enter the nos"); scanf("%d%d",&a,&b); t=a; a=b; b=t; printf("the nos after swapping", a,b); getch(); } Source: CoolInterview.com
Answered by: jayasree | Date: 8/27/2010
| Contact jayasree
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|