• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc
Tech Stack Journal

Tech Stack Journal

Algorithm and Flowchart to Swap Two Numbers

August 15, 2019 by Admin Leave a Comment

Contents

  • 1 Understanding the approach of Swapping Two Numbers
  • 2 Using Temporary Variable
    • 2.1 Pseudocode
    • 2.2 Flowchart
  • 3 Without Using Temporary Variable
    • 3.1 Pseudocode
    • 3.2 Flowchart
  • 4 Swap Two Numbers in Java
    • 4.1 Using a Temporary Variable
    • 4.2 Without Using the Temporary Variable
    • 4.3 Using Bitwise Operators
  • 5 Swap Two Numbers in C language
    • 5.1 Using a Temporary Variable:
    • 5.2 Without Using Temporary Variable
    • 5.3 Using Pointers within a Function
    • 5.4 Using Bitwise Operators
  • 6 Swap Two Numbers in Python
    • 6.1 Using a Temporary Variable
    • 6.2 Without Using Temporary Variable
    • 6.3 Using Bitwise Operators

In this post, we will see examples on how to swap two numbers using a temporary variable and without using a temporary variable.

Understanding the approach of Swapping Two Numbers

Imagine you have red marbles in green glass and green marbles in red glass. How do you interchange the marbles so that red marbles will be in red glass and green marbles will be in green glass?

One way is to take one more empty white glass and pour the green marbles of red glass into it. Pour the red marbles from green glass into red glass and then pour the green marbles from white glass into green glass.

Another way would be to first pour red marbles of green glass into red glass. Now red glass will have both red and green marbles. Then remove green marbles from the red glass and place it in green glass.

Similar to the above analogy, we can swap two numbers in two ways.

  1. Using temporary variable
  2. Without using temporary variable

Using Temporary Variable

First way is to store one of the variable value, say A’s value in another memory location, a temporary third variable T. Then copy B’s value into A and then T’s value into A. In this approach we need an additional storage to store the value of a number.

A = 5
B = 10
T = A   // A=5,  B=10, T=5
A = B   // A=10, B=10, T=5
B = T   // A=10, B=5,  T=5

Pseudocode

STEP 1: READ A, B
STEP 2: T = A
STEP 3: A = B
STEP 4: B = T
STEP 5: PRINT A, B

Flowchart

Swap Two Numbers Using Temporary Variable

Without Using Temporary Variable

Second way is to add both A and B, and store the result in A. Now A has both A’s value and B’s value. If I want only A’s value, I can subtract B from A. I’ll store this result in B. We’ve solved half of the problem. Now, we need B’s value in A. For that, we can now subtract B’s value (which actually has A’s value) from A, with this B’s value will remain in A.

A = 5
B = 10
A = A + B   // A=15, B=10
B = A - B   // A=15, B=5
A = A - B   // A=10, B=5

Pseudocode

STEP 1: READ A, B
STEP 2: A = A + B
STEP 3: B = A - B
STEP 4: A = A - B
STEP 5: PRINT A, B

Flowchart

Swap Two Numbers Without Using Temporary Variable

Swap Two Numbers in Java

Using a Temporary Variable

package com.techstackjournal;

public class SwapNumbersExample1 {
	public static void main(String[] args) {
		int a = 5;
		int b = 10;
		int t;
		t = a;
		a = b;
		b = t;
		System.out.println("a = " + a);
		System.out.println("b = " + b);
	}
}

Without Using the Temporary Variable

package com.techstackjournal;

public class SwapNumbersExample2 {
	public static void main(String[] args) {
		int a = 5;
		int b = 10;
		a = a + b;
		b = a - b;
		a = a - b;
		System.out.println("a = " + a);
		System.out.println("b = " + b);
	}
}

Using Bitwise Operators

package com.techstackjournal.java.basics;

public class SwapNumbersExample3 {

	public static void main(String[] args) {

		int a = 5;
		int b = 10;

		a = a ^ b;
		b = a ^ b;
		a = a ^ b;

		System.out.println("a = " + a);
		System.out.println("b = " + b);
	}

}

Swap Two Numbers in C language

Using a Temporary Variable:

#include <stdio.h>

int main()
{
    int a = 5;
    int b = 10;
    int t;
    t = a;
    a = b;
    b = t;
    printf("\n a = %d", a);
    printf("\n b = %d", b);
    return 0;
}

Without Using Temporary Variable

#include <stdio.h>

int main()
{
    int a = 5;
    int b = 10;
    a = a + b;
    b = a - b;
    a = a - b;
    printf("\n a = %d", a);
    printf("\n b = %d", b);
    return 0;
}

Using Pointers within a Function

#include <stdio.h>

void swap(int *a, int *b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

int main()
{
    int a = 5;
    int b = 10;
    swap(&a,&b);
    printf("\n a = %d", a);
    printf("\n b = %d", b);
    return 0;
}

Using Bitwise Operators

#include <stdio.h>

int main()
{
    int a = 5;
    int b = 10;
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
    printf("\n a = %d", a);
    printf("\n b = %d", b);
    return 0;
}

Swap Two Numbers in Python

Using a Temporary Variable

a = 5;
b = 10;
t = a
a = b
b = t
print ("a = ", a)
print ("b = ", b)

Without Using Temporary Variable

a = 5;
b = 10;
a = a + b
b = a - b
a = a - b
print ("a = ", a)
print ("b = ", b)

Using Bitwise Operators

a = 5;
b = 10;
a = a ^ b
b = a ^ b
a = a ^ b
print ("a = ", a)
print ("b = ", b)

Filed Under: Algorithms

Next Post: Palindrome Numbers »

Primary Sidebar

More to See

Arrays.asList in Java Examples

February 21, 2021 By Admin

[Solved] Why List.add throws UnsupportedOperationException in Java?

February 20, 2021 By Admin

Secondary Sidebar

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • Misc
  • Node.js
  • Spring Core
  • Windows

Archives

  • February 2021 (6)
  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

Navigation

  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc

Recent

  • How to Make File Explorer Open to This PC instead of Quick Access in Windows 10
  • Arrays.asList in Java Examples
  • [Solved] Why List.add throws UnsupportedOperationException in Java?
  • How to Convert an Array to List in Java?
  • How Many Spaces in a Tab?

Search

Copyright © 2021 · Tech Stack Journal · Log in