How to swap two variables, without using third variable ?

Answers were Sorted based on User's Feedback



How to swap two variables, without using third variable ?..

Answer / xyz

Congrats to all guys who have tried this.Everything u post
here is correct

Is This Answer Correct ?    0 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / not so good coder

I heard you put them into an excel spreadsheet, and just
move one cell over the other.

Is This Answer Correct ?    0 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / adarsh jain

We can do it using pointers easily...

See the code snippets below..

#include<stdio.h>

void swap(int *a, int *b);
int main()
{
int a = 10;
int b = 20;
swap(&a, &b);
return 0;
}

void swap(int *a, int *b)
{
printf("Before swapping , a = %d, b = %d\n", *a ,
*b);
*(a+1) = *a;
*a = *b;
*b = *(a+1);
printf("After Swapping, a = %d, b = %d\n", *a, *b);
}

Is This Answer Correct ?    0 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / saad bin saulat

Full code to swap variables without using a temporary variable is available at the below mentioned link:
http://bitsbyta.blogspot.com/2011/01/swapping-values-without-third-variable.html

Is This Answer Correct ?    1 Yes 1 No

How to swap two variables, without using third variable ?..

Answer / zee hassan

#include<iostream.h>
#include<conio.h>
main()
{
int a,b;
a=5;
b=10;
a=a+b; /*a=5+10=15*/
b=a-b; /*b=15-10=5*/
a=a-b;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
getch();
}

Is This Answer Correct ?    0 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / swathi

a=5 b=10
b=b-a, a=a+b
b=10-5=5
a=5+5=10
b=5,a=10

Is This Answer Correct ?    1 Yes 1 No

How to swap two variables, without using third variable ?..

Answer / aditi agrawal

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

Is This Answer Correct ?    1 Yes 1 No

How to swap two variables, without using third variable ?..

Answer / ipsagel

# 3 also

Is This Answer Correct ?    0 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / saurabh singh

a=b^a^(b=a);

Is This Answer Correct ?    0 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / aditya raj

1st and 2nd methods r right!!
dere iz some problem wid 3rd..if a=0.

18th method...how u guy give dis type of solution?? is it
ryte?? suppose a>b den??

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Code Interview Questions

main() { unsigned int i=65000; while(i++!=0); printf("%d",i); }

1 Answers  


enum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); }

2 Answers  


main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); }

1 Answers   Zoho,


Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.

1 Answers  


How we print the table of 3 using for loop in c programing?

7 Answers  






main() { int i =10, j = 20; clrscr(); printf("%d, %d, ", j-- , --i); printf("%d, %d ", j++ , ++i); } a. 20, 10, 20, 10 b. 20, 9, 20, 10 c. 20, 9, 19, 10 d. 19, 9, 20, 10

4 Answers   HCL,


create a C-code that will display the total fare of a passenger of a taxi if the driver press enter,the timer will stop. Every 10 counts is 2 pesos. Initial value is 25.00

0 Answers   Microsoft,


What is the main difference between STRUCTURE and UNION?

13 Answers   HCL,


main() { printf("%d, %d", sizeof('c'), sizeof(100)); } a. 2, 2 b. 2, 100 c. 4, 100 d. 4, 4

18 Answers   HCL, IBM, Infosys, LG Soft, Satyam,


void main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } }

1 Answers  


void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); }

3 Answers   Accenture,


To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates.

19 Answers   Amazon, BITS, Microsoft, Syncfusion, Synergy, Vector,


Categories