How to swap two variables, without using third variable ?
Answers were Sorted based on User's Feedback
Answer / xyz
Congrats to all guys who have tried this.Everything u post
here is correct
| Is This Answer Correct ? | 0 Yes | 0 No |
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 |
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 |
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 |
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 |
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 |
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 |
main() { int i; float *pf; pf = (float *)&i; *pf = 100.00; printf("\n %d", i); } a. Runtime error. b. 100 c. Some Integer not 100 d. None of the above
#include<stdio.h> main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } }
void main() { int x,y=2,z; z=(z*=2)+(x=y=z); printf("%d",z); }
4. Main() { Int i=3,j=2,c=0,m; m=i&&j||c&I; printf(“%d%d%d%d”,I,j,c,m); }
#define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); }
how to swap 3 nos without using temporary variable
How do you write a program which produces its own source code as its output?
#define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); }
Write a c program to search an element in an array using recursion
main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); }
Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal.
6 Answers Fusion Systems GmbH,
String copy logic in one line.