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

Does c have enums?

0 Answers  


What is extern variable in c with example?

0 Answers  


How do I send escape sequences to control a terminal or other device?

0 Answers  


in iso what are the common technological language?

0 Answers  


Hai,I have done with my bachelor of commerce and planing to ms,please suggest me how to convince vo for shifting from commerce to computers. Visa on 8 DEC 2014  Npu university

0 Answers  






"%u" unsigned integer print the a) address of variable b) value of variable c) name of a variable d) none of the above

0 Answers  


What are the uses of pre-processor directives?

2 Answers  


C program to find frequency of each character in a text file?

6 Answers  


Write a program to implement queue.

0 Answers   Aricent,


Differentiate between ordinary variable and pointer in c.

0 Answers  


What is the output of below code? main() { static in a=5; printf("%3d",a--); if(a) main(); }

4 Answers   Infosys, TCS,


There are N egg baskets and the number of eggs in each basket is a known quantity. Two players take turns to remove these eggs from the baskets. On each turn, a player must remove at least one egg, and may remove any number of eggs provided they all belong to the same basket. The player picking the last egg(s) wins the game. If you are allowed to decide who is going to start first, what mathematical function would you use to decide so that you end up on the winning side? Upload a C program to demonstrate the behaviour of the game.

2 Answers  


Categories