write a program in C to swap two variables

Answers were Sorted based on User's Feedback



write a program in C to swap two variables..

Answer / muzammil

#include <stdio.h>
#include <conio.h>

main()
{
int a,b;
printf("Enter the values: ");
scanf("%d%d",&a,&b);
printf("The values before swapping: %d %d",a,b);
a=a-(b=(-b+(a=a+b)));
printf("The values after swapping are: %d %d",a,b);
getch();
}

Is This Answer Correct ?    14 Yes 3 No

write a program in C to swap two variables..

Answer / r.aruna

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the a value");
scanf("%d",&a);
printf("Enter the b value");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("After swapping a,b value",a,b);
getch();
}

Is This Answer Correct ?    11 Yes 3 No

write a program in C to swap two variables..

Answer / muzammil

Here are some LOGICS to swap two nmbrs

1) a+=b; b=a-b; a-=b;
2) a*=b; b=a/b; a/=b;
3) a=a^b; b=a^b; a=a^b;

Now Single line statements

4) a=a-(b=(-b+(a=a+b)));
5) a=a^(b=(b^(a=a^b)));
6) b=a+b-(a=b); This one was posted BY Senthil Thanks to him
7) b=a*b/(a=b); Dis is same as above one except for da
operators.

Is This Answer Correct ?    5 Yes 0 No

write a program in C to swap two variables..

Answer / priyamurugan

#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
printf("\n BEFORE SWAPPING THE NOS");
printf("\n enter the a, b values");
scanf("%d %d",&a,&b);
swap(&a,&b);
printf("\n AFTER SWAPPING THE NOS");
printf("\n a=%d",a);
printf("\n b=%d",b);
getch();
}
swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
return();
}

Is This Answer Correct ?    5 Yes 2 No

write a program in C to swap two variables..

Answer / srinivas

#include <stdio.h>

int main(void)
{
int a = 2, b = 5;

a = a + b;
b = a - b;
a = a - b;
printf("%d\t%d\n", a, b);
return 0;
}

Is This Answer Correct ?    2 Yes 0 No

write a program in C to swap two variables..

Answer / senthil mca sns

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the a value:");
scanf("%d",&a);
printf("Enter the b value:");
scanf("%d",&b);
b=a+b-(a=b);
printf("After Swapping a=%d,b=%d",a,b);
getch();
}

Is This Answer Correct ?    3 Yes 2 No

write a program in C to swap two variables..

Answer / anju

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
scanf("%d%d",&a,&b);
printf("before swapping value of a=%d and b=%d",a,b);
temp=x;
x=y;
y=temp;
printf("after swapping value of a=%d and b=%d",a,b);
getch();
}

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More C Interview Questions

Explain the advantages and disadvantages of macros.

0 Answers   TCS,


Explain the difference between getch() and getche() in c?

0 Answers  


a c variable cannot start with a) an alphabet b) a number c) a special symbol d) both b and c above

0 Answers  


a linearly ordered set of data elements that have the same structure and whose order is preserved in storage by using sequential allocation a) circular b) ordinary c) array d) linear list

0 Answers  


Why is #define used?

0 Answers  






FIND THE OUTPUT IF THE INPUT IS 5 5.75 void main() { int i=1; float f=2.25; scanf("%d%f",&i,&f); printf("%d %f",,i,f); } ANSWER IS 5 AND 2.25 WHY?

4 Answers   Wipro,


Q-1: Create a structure to specify data on students given below: Roll number, Name, Department, Course, Year of joining Assume that there are not more than 450 students in the college.

8 Answers  


Is c weakly typed?

0 Answers  


main() { int arr[5]={23,67}; printf("%d%d%d",arr[2],arr[3],arr[4]); }

9 Answers   TCS,


A B C D E F G F E D C B A A B C D E F F E D C B A A B C D E E D C B A A B C D D C B A A B C C B A A B B A A A

2 Answers  


What is the hardest programming language?

0 Answers  


write a program in c to find out the sum of digits of a number.but here is a condition that compiler sums the value from left to right....not right to left..

1 Answers  


Categories