Program to swap two numbers without using the third variable

Program to swap two number without using third variable.Most asked question in interview.

--

Program using JavaScript

var a=window.prompt('enter a number')
var b=window.prompt('enter a number')
var a=a+b;
var b=a-b;
var a=a-b;
console.log ("value of a is",a);
console.log('value of b is',b);

Algorithm

Step 1: Start

Step 2: Enter two numbers a and b

Step 3: Print a, b

Step 4: a= a+b

Step 5: b= a-b

Step 6: a= a-b

Step 7: End

Output

enter a number a and b : 10,20
Before Swapping :10,20
After Swapping :20,10

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

puja sharma
puja sharma

No responses yet

Write a response