TCS
Company
Programming
Variables
Write a program to swap two numbers without using a temporary
variable.
Read Solution (Total 25)
-
- #include
#include
int main()
{
int x = 10, y = 5;
printf("numbers are x=%d ,y=%d",x,y);
// Code to swap 'x' and 'y'
x = x + y; // x now becomes 15
y = x - y; // y becomes 10
x = x - y; // x becomes 5
printf("After Swapping: x = %d, y = %d", x, y);
return 0;
} - 6 years agoHelpfull: Yes(6) No(0)
- void main()
{
int a,b;
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("%d %d",a,b);
} - 6 years agoHelpfull: Yes(2) No(0)
- #include
int main()
{
int a=10, b=15;
a=a+b-(b=a);
printf("a=%d b=%d" ,a, b);
return 0;
} - 6 years agoHelpfull: Yes(2) No(0)
- we can write like this
a=a+b
b=a-b
c=a-b
or u can change the given numbers into binary and u can use a logic
A=A XOR B
B=A XOR B
A=A XOR B - 6 years agoHelpfull: Yes(1) No(0)
- sry there was a change in first logic
a=a+b
b=a-b
a=a-b - 6 years agoHelpfull: Yes(1) No(0)
- method 1: Use XOR operation
a=a^b
b=a^b
a=a^b
method 2:
a=a+b
b=a-b
a=a-b - 6 years agoHelpfull: Yes(1) No(0)
- #include
int main() {
int x=10,y=15;
printf("before swaping numbers a=%d, b=%d",x,y);
x=x+y;
y=x-y;
x=x-y;
printf("nafter swaping number is a=%d , b=%d",x,y);
return 0;
}
output:-
before swaping numbers a=10, b=15
after swaping number is a=15 , b=10 - 6 years agoHelpfull: Yes(1) No(0)
- #include
#include
void
main ()
{
int a, b;
printf ("Enter a and b : ");
scanf ("%d%d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf ("nnAfter Swapping a = %d , b = %d", a, b);
} - 6 years agoHelpfull: Yes(0) No(0)
- a=a+b;b=a-b;a=a-b;
- 6 years agoHelpfull: Yes(0) No(0)
- use concept---
int a=5,b=6;
a=a+b;
b=a-b;
a=a-b; - 6 years agoHelpfull: Yes(0) No(0)
- b=a+b;
a=b-a;
b=b-a; - 6 years agoHelpfull: Yes(0) No(0)
- void main()
{
int a,b,t;
a=5;
b=6;
swap(int a,int b)
{
t=a;
a=b;
b=t;
} - 6 years agoHelpfull: Yes(0) No(4)
- a = a+b
b = a-b
a = a-b - 6 years agoHelpfull: Yes(0) No(0)
- without using the third variable
> take the input and b.
>put, a=a+b;
>put,b=a-b;
>put, a=a-b;
.print a and b - 6 years agoHelpfull: Yes(0) No(0)
- This question can be easily solved using boolean 'xor' operation
//let a=2, b=3
a= a^b; //a=1 (2 xor 3)
b= a^b; //b=2 (1 xor 3)
a= a^b; //a=3 (1 xor 2)
//Hence swapped - 6 years agoHelpfull: Yes(0) No(0)
- value1=40 , value2=30
value1=(value1+value2)-value1
value2=(value1+value2)-value2 - 6 years agoHelpfull: Yes(0) No(0)
- #include
int main()
{
int x=12;
int y=10;
x=x*y;
y=x/y;
x=x/y;
printf("After swaping:n x=%d y=%d",x,y);
} - 6 years agoHelpfull: Yes(0) No(0)
- a=a+b;
b=a-b;
a=a-b; - 6 years agoHelpfull: Yes(0) No(0)
- a=a+b;
b=a-b;
a=a-b; - 6 years agoHelpfull: Yes(0) No(0)
- int a,b,c;
a= 5;
b=10;
a = a + b;
b = a - b;
a = a - b; - 6 years agoHelpfull: Yes(0) No(0)
- Python :
x,y = y,x
print(x)
print(y) - 6 years agoHelpfull: Yes(0) No(0)
- //python implementation:
a=3
b=5
print("before swapping a is "+ str(a) +" & b is " + str(b) )
a,b=b,a
print("after swapping a is "+ str(a) +" & b is " + str(b) ) - 6 years agoHelpfull: Yes(0) No(0)
- Let the numbers be x and y,
x=x+y;
y=x-y;
x=x-y; - 5 years agoHelpfull: Yes(0) No(0)
- #include
int main()
{
int n1=3,n2=4;
printf("after swape %d ,%d"n1,n2);
n1=n1+n2;
n2=n1-n2;
n1=n1-n2;
printf("after swape %d ,%d"n1,n2);
return 0;
} - 5 years agoHelpfull: Yes(0) No(0)
- #include
int main()
{
int a,b;
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("%d%d",a,b);
return 0;
} - 5 years agoHelpfull: Yes(0) No(0)
TCS Other Question