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

What is oops c?

0 Answers  


The operation of a stair case switch best explains the a) or operation b) and operation c)exclusive nor operation d)exclusive or operation Which of the following is/are syntactically correct? a) for(); b) for(;); c) for(,); d) for(;;);

1 Answers   HCL, Public Service Commission,


What is the use of linkage in c language?

0 Answers  


Function which gives a pointer to a binary trees const an integer value at each code, return function of all the nodes in binary tree.?

0 Answers   Infosys,


What is getch () for?

0 Answers  






Are pointers integer?

0 Answers  


What is the difference between void main and main in c?

0 Answers  


read an array and search an element

1 Answers  


how to calculate the time complexity of a given algorithm? pls give exaples..mainly for the coplexities such as O(log n),O(n log n)...

1 Answers   Infosys,


What is the significance of an algorithm to C programming?

0 Answers  


What is #include called?

0 Answers  


Is null equal to 0 in sql?

0 Answers  


Categories