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

What is the use of a ‘’ character?

0 Answers  


char p="data"; printf(p);

2 Answers  


write a C and C++ programme to implement the A,bubble sort B,quick sort C,insertion sort D,sequential search E,binary search

1 Answers   ADP, TCS,


How can I change their mode to binary?

0 Answers  


which types of data structure will i use to convert infix to post fix???

5 Answers   IIT,






What is the process of writing the null pointer?

0 Answers  


what is c++ programming?

3 Answers   TCS,


A set of N billiard balls are set on a one-dimensional table. The table is 1 meter long, set north-south with two pockets at either side. Each ball has zero width and there is no friction so it is moving with a fixed velocity of either northward or southward and bounces back in a perfect elastic collision from other balls it encounter on its way (or drop into one of the pockets). Your job is to keep track of the balls movements. Task Please write a program that gets the initial place, speed and direction of all the balls and gives the position of a specific ball after t seconds. Input The first line contains the number of scenarios. Each one of the other lines in the input contains a scenario: The first number, N, is the number of balls; followed by N pairs of numbers: the distance in centimeters from the south end of the table and the speed (positive speed meaning it moves northward); the last two numbers are the number i of the target ball you should track and the time T in seconds. Output The output is a single number for each line which is the place (distance in centimeters from the south end of the table) of the tracked ball after T seconds. Note: There is no new line character at the end of the result. Sample Input 5 1 50 1 1 1000 1 50 1 1 6 1 60 -2 1 6 2 10 1 95 -1 2 30 2 10 1 95 -1 2 60 Sample Output 100 56 48 65 70

0 Answers  


write a program that will accept two integers and will implement division without using the division operator if the second value is an odd number and will implement multiplication without using multiplication operator if the second value is an even number.

1 Answers  


#include<stdio.h> #include<conio.h> void main() { int m=0111,n=20; printf("%d%d\n",m,n); getch(); }

1 Answers  


write a program to concatenation the string using switch case?

0 Answers  


How pointers are declared?

0 Answers  


Categories