write a program to swap two numbers without using temporary
variable?

Answers were Sorted based on User's Feedback



write a program to swap two numbers without using temporary variable?..

Answer / it diploma student

let a and b be the variables

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

if we take a=5 and b = 7 then

a = 5+7 = 12
b = 12-7 = 5
a = 12-5 = 7

hence swaped...

Is This Answer Correct ?    26 Yes 3 No

write a program to swap two numbers without using temporary variable?..

Answer / guest

[code]

main()
{
int a=10;
int b=20;
printf("a = %d\nb = %d\n",a,b);
a =a+b; b=a-b; a=a-b; // This is first way.

a =a*b; b=a/b; a=a/b;
printf("a = %d\nb = %d\n",a,b);
}
[/code]

[code]
main()
{
int a=10;
int b=20;
printf("a = %d\nb = %d\n",a,b);
a =a*b; b=a/b; a=a/b; //This is second way
printf("a = %d\nb = %d\n",a,b);
}
[/code]




Is This Answer Correct ?    19 Yes 2 No

write a program to swap two numbers without using temporary variable?..

Answer / sam

let a and b be variables
a^=b^=a^=b;
by performing this logic from right hand side we can swap
two variables

Is This Answer Correct ?    6 Yes 5 No

Post New Answer

More C Interview Questions

write a c program thal will find all sequences of length N that produce the sum is Zero, print all possible solutions?

0 Answers   Wipro,


What is wrong with this code such that it doesnt produce the input reversed? #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char Space = ' '; char LineOfText; float count; LineOfText = getchar(); while ((LineOfText = getchar()) != '/n'); { count = strlen(LineOfText) - 1; while (count >= 0) { putchar(LineOfText[count]); count--; } } getchar(); return 0; }

2 Answers  


Difference between goto, long jmp() and setjmp()?

0 Answers   EXL,


Sir,please help me out with the output of this programme:- #include<stdio.h> #include<conio.h> void main() { int a=18,b=12,i; for(i=a<b?a:b;a%i||b%i;i--); printf("%d %d",i); }

4 Answers  


How old is c programming language?

0 Answers  






How can I write data files which can be read on other machines with different word size, byte order, or floating point formats?

0 Answers  


code for selection sort?

1 Answers  


What does it mean when a pointer is used in an if statement?

0 Answers  


how can make variable not in registers

1 Answers   TCS,


Why is event driven programming or procedural programming, better within specific scenario?

0 Answers   Wipro,


What is pointer to pointer in c with example?

0 Answers  


Give the Output : * * * * * * * * * *

2 Answers  


Categories