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() { char not; not=!2; printf("%d",not); }
#include<stdio.h> #include<conio.h> void main() { int a=(1,2,3,(1,2,3,4); switch(a) { printf("ans:"); case 1: printf("1");break; case 2: printf("2");break; case 3: printf("1");break; case 4: printf("4");break; printf("end"); } getch(); }
int i,j; for(i=0;i<=10;i++) { j+=5; assert(i<5); }
main() { int x=5; for(;x!=0;x--) { printf("x=%d\n", x--); } } a. 5, 4, 3, 2,1 b. 4, 3, 2, 1, 0 c. 5, 3, 1 d. none of the above
main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcat(a,b)); } a. Hello b. Hello World c. HelloWorld d. None of the above
how can i search an element in an array
2 Answers CTS, Microsoft, ViPrak,
find A^B using Recursive function
main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; }
Is the following code legal? typedef struct a aType; struct a { int x; aType *b; };
main() { char *cptr,c; void *vptr,v; c=10; v=0; cptr=&c; vptr=&v; printf("%c%v",c,v); }
How can I Create a C program in splitting set of characters to specific subsets. Example: INPUT SET OF CHARACTERS: Therefore, my dear brothers and sisters, stand firm. Let nothing move you. Always give yourselves fully to the work of the Lord, because you know that your labor in the Lord is not in vain. SPLIT INTO HOW MANY CHARACTERS PER SUBSETS: 10 OUTPUT: Therefore, my dear b rothers an d sisters, stand fir m. Let not hing move you. Alway s give you rselves fu lly to the work of t he Lord, b ecause you know that your labo r in the L ord is not in vain.
main() { char c=' ',x,convert(z); getc(c); if((c>='a') && (c<='z')) x=convert(c); printf("%c",x); } convert(z) { return z-32; }