CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
Code for swapping of two numbers without using temporary variable using C.

Posted by: kalpana on 11/15/2007

Contact kalpana  Contact kalpana
Category C Interview Questions
Rating (4.9) By 8 users
Added on 11/15/2007
Views 4585
Rate it!
Answers:

int a=10,b=5;

a=a+b; // a=15
b=a-b; // b=10
a=a-b; // a=5



 Posted by: Mohan    

Contact Mohan  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");
}



 Posted by: sukavasi suresh    

Contact sukavasi suresh  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();
}



 Posted by: sourav    

Contact sourav  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);
}



 Posted by: tirumalarao peddineni    

Contact tirumalarao peddineni  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
======



 Posted by: Ashish    

Contact Ashish  Contact Ashish

int a=10, b=20;
a=a+b;
b=a-b;
a=a-b;



 Posted by: Ashish    

Contact Ashish  Contact Ashish

code is :
b=a+b;
a=b-a;
b=b-a;



 Posted by: murali    

Contact murali  Contact murali

a,b
a=a+b
b=a-b
a=a-b
now a and b are swapped



 Posted by: girish    

Contact girish  Contact girish

int i=9,j=8;
i=i+j;
j=i-j;
i=i-j;
printf("J = ",&j);
System.out.println("I =",&i);



 Posted by: bablu    

Contact bablu  Contact bablu

int a = 10;
int b = 20;

b = a + b;
a = b - a;
b = b - a;
printf ("a = %d, b = %d
", a, b);



 Posted by: Nathan    

Contact Nathan  Contact Nathan

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



 Posted by: topcoder    

Contact topcoder  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
}



 Posted by: Sarwat    

Contact Sarwat  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



 Posted by: SUKHCHAIN SANDHU    

Contact SUKHCHAIN SANDHU  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);
}



 Posted by: Jai    

Contact Jai  Contact Jai

one method :

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


other method :
a^=b^=a;



 Posted by: vikas kr    

Contact vikas kr  Contact vikas kr

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



 Posted by: Vikas    

Contact Vikas  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;
}



 Posted by: Syed Baseer Ahmed    

Contact Syed Baseer Ahmed  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);



 Posted by: praveen.c    

Contact praveen.c  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;
}



 Posted by: praveen.c    

Contact praveen.c  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;



 Posted by: Satish Aradhya    

Contact Satish Aradhya  Contact Satish Aradhya

a,b are two variables...
a=a+b;
b=a-b;
a=a-b;



 Posted by: Naga Bhushanam    

Contact Naga Bhushanam  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);
}



 Posted by: uppala mamatha    

Contact uppala mamatha  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();
}



 Posted by: Jeevan K.M.    

Contact Jeevan K.M.  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



 Posted by: sunanda    

Contact sunanda  Contact sunanda

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



 Posted by: vijay jain    

Contact vijay jain  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;



 Posted by: Manoj Kumar    

Contact Manoj Kumar  Contact Manoj Kumar

main()
{
int x,y;
temp z;
x = y;
y = z;
z = x;
printf("%d%d",x,y);
}



 Posted by: D.N.Pani    

Contact D.N.Pani  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);
}



 Posted by: lokesh    

Contact lokesh  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;

}



 Posted by: lokesh    

Contact lokesh  Contact lokesh

use, 2 variables a, b.then try this code,

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

that's it



 Posted by: nethaji    

Contact nethaji  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 */



 Posted by: sudarsan dash    

Contact sudarsan dash  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



 Posted by: Sagar    

Contact Sagar  Contact Sagar

int a,b;
a=a^b;
b=a^b;
a=a^b;



 Posted by: vish    

Contact vish  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;
}



 Posted by: Essam Ali    

Contact Essam Ali  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);
}



 Posted by: Ram    

Contact Ram  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;



 Posted by: bharath    

Contact bharath  Contact bharath

If a and b are two variables then we can swapped with this code
a=a*b/(b=a)



 Posted by: DHEERAJ SHARMA    

Contact DHEERAJ SHARMA  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);
}



 Posted by: venkatarathnam.m.v    

Contact venkatarathnam.m.v  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();
}



 Posted by: mahila    

Contact mahila  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);



 Posted by: ravindrabonthu    

Contact ravindrabonthu  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);
}



 Posted by: Praveen    

Contact Praveen  Contact Praveen

a=10;
b=20
b=((a+b)-(a=b))//in a single line



 Posted by: Debasis Nayak    

Contact Debasis Nayak  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();
}



 Posted by: GAUATM DESHPANDE.    

Contact GAUATM DESHPANDE.  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);
}



 Posted by: Govind    

Contact Govind  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;



 Posted by: sushma    

Contact sushma  Contact sushma


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
code To draw a three dimensional graph using c graphics
View Answer
How to write a C program for displaying a sentence without output command?
View Answer
What is difference between the test effort and the test procedure?
View Answer
What are the disadvantages of using Pointers.
View Answer
How can I convert a number to a string?
View Answer
How to write a program for pascal triangle?
View Answer
write a program to remove comment lines and blank lines from an error free c program.
View Answer
What use of structure and union?
View Answer
Where does global, static, local, register variables, free memory and C Program instructions get stored?
View Answer
1)What is static identifier?


2)Where are the auto variables stored?
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/12459/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.98