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 the program for displaying the ten most frequent words in a file such that your program should be efficient in all complexity measures.

3 Answers   Google,


How do I use void main?

0 Answers  


What is the output for the below program? void main() { float me=1.1; double you=1.1; if(me==you) printf("love c"); else printf("know c"); }

7 Answers  


show how link list can be used to repersent the following polynomial i) 5x+2

0 Answers   CBSE,


What is the auto keyword good for?

0 Answers  


what is the format specifier for printing a pointer value?

0 Answers  


What is the OOPs concept?

3 Answers  


What is pointer in c?

0 Answers  


Can anyone help me with this please? Need to print the below values.. Thanks 1 1 2 1 2 3 1 2 3 4

3 Answers  


State the difference between x3 and x[3].

0 Answers   Aricent,


How variables are declared in c?

0 Answers  


Write a program to find the given number is odd or even without using any loops(if,for,do,while)

4 Answers   CNC, Gokul,


Categories