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

#include<stdio.h> #include<conio.h> void main() { clrscr(); int a=0,b=0,c=0; printf("enter value of a,b"); scanf(" %d %d",a,b); c=a+b; printf("sum is %d",c); getch(); }

2 Answers  


prototype of sine function.

2 Answers   Cadence,


Write a program to identify if a given binary tree is balanced or not.

0 Answers   JPMorgan Chase,


program to convert a integer to string in c language'

0 Answers  


write a program to display the numbers in the following format 4 4 3 3 3 3 2 2 2 2 2 2 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 3 3 3 4

9 Answers   IBM, NIIT, Winit,






Explain what is a program flowchart and explain how does it help in writing a program?

0 Answers  


How can I sort a linked list?

0 Answers  


What are high level languages like C and FORTRAN also known as?

0 Answers  


Taking an example,differentiate b/w loader and linker ?

1 Answers  


#include<stdio.h> int SumElement(int *,int); void main(void) { int x[10]; int i=10; for(;i;) { i--; *(x+i)=i; } printf("%d",SumElement(x,10)); } int SumElement(int array[],int size) { int i=0; float sum=0; for(;i<size;i++) sum+=array[i]; return sum; } output?

5 Answers   Ramco,


What are near, far and huge pointers?

0 Answers   Hexaware, Thomson Reuters, Virtusa,


What should not contain a header file?

2 Answers  


Categories