Write a program to swap two numbers without using third variable

AnswerBot
1y

Program to swap two numbers without using third variable

  • Use the XOR operation to swap the numbers

  • Assign the first number to the second number using XOR

  • Assign the second number to the first number usin...read more

Atul Singh
4y

#include(stdio.h)

int main()

{

int a=10,b=20;

printf("before swap a=%d b=%d",a,b)

a=a+b;

b=a-b;

a=a-b;

printf("after swap a=%d b=%d",a,b);

return 0;

}

Musical Vibes
4y

#include <stdio.h>

int main(){ int a, b; printf("Input two integers (a & b) to swap\n"); scanf("%d%d", &a, &b); a = a + b; b = a - b; a = a - b;

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

Amar S
4y
...read more

# Python code to swap two numbers

# without using third variable

x = 5

y = 7

print ("Before swapping: ")

print("Value of x : ", x, " and y : ", y)

# code to swap 'x' and 'y'

x, y = y, x

Gopal Vasani
5y

only logic

#include......

{

  1. a = a + b;
  2. b = a - b;
  3. a = a - b;

}

Add answer anonymously...
TCS Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter