Share

Carbon program to swap two numbers

Home » Programming Language » Carbon » Carbon program to swap two numbers
program to swap two numbers in carbon programming language

Here, we will see how to swap two numbers using a Carbon program. Below are the examples:

Input : x = 10, y = 5 
Output :  x = 5, y = 10

Input : x = 1, y = 10 
Output : x = 10, y = 1

In this we’re going to learn about three ways to swap two numbers in Carbon, and those are mentioned below:

  1. Using a temp variable.
  2. Without using a temp variable.
  3. Using Bitwise Operator.

Let’s start discussing each of these methods in detail.

1. Using a temp variable

The idea is simple for this approach for swapping two numbers:

  1. Assign x variable to a temp variable: temp = x
  2. Assign y variable to x variable: x = y
  3. Assign temp variable to y variable: y = temp

Below is the Carbon program to implement the Swapping with temp variable approach:

package sample api;

fn Main() -> i32 {

    // using temp variable
    var x: i32 = 1;
    var y: i32 = 2;
    var temp: i32 = x;
    x = y;
    y = temp;


    Print("SWAPPING");
    Print("x: {0}", x);
    Print("y: {0}", y);

    return 0;
}

Output:

SWAPPING
x: 2
y: 1

2. Without using temp variable

Approach:

Simple idea behind this code is to use arithmetic operators. We will take the sum of the two numbers and store it in one number and store the difference of both the numbers in the other number. Finally we will store the difference of both the numbers in the first number. Since the values will be updated after the first two operations we will be able to swap the numbers.

Algorithm:

  1. Assign to y the sum of x and b i.e. y = x + y.
  2. Assign to x difference of y and x i.e. x = y – x.
  3. Assign to y the difference of y and x i.e. y = y – x.

Below is the Carbon program to implement the Swapping without temp variable approach:

package sample api;

fn Main() -> i32 {

    // without temporary variable
    var x: i32 = 10;
    var y: i32 = 2;

    y = x + y;
    x = y - x;
    y = y - x;

    Print("SWAPPING");
    Print("x: {0}", x);
    Print("y: {0}", y);

    return 0;
}

Output:

SWAPPING
x: 2
y: 10

3. Using Bitwise Operator

The following approach will be used here:

  1. Assign to “x” the XOR of “x” and “y” i.e. x = x ^ y.
  2. Assign to “y” the XOR of “y” and “x” i.e. y = y ^ x. 
  3. Assign to y the difference of “y” and “x” i.e. x = x ^ y.

Below is the Carbon program to implement the Swapping by Bitwise Operator approach:

package sample api;

fn Main() -> i32 {


    // using bitwise 
    // operator
    var x: i32 = 5;
    var y: i32 = 12;

    Print("before swapping");
    Print("x: {0}", x);
    Print("y: {0}", y);

    x = x ^ y;
    y = y ^ x; // y = b ^ (x ^ y) = x
    x = y ^ x; // a = (x ^ y) ^ x = y
    
    Print("after swapping");
    Print("x: {0}", x);
    Print("y: {0}", y);

    return 0;
}

Output:

before swapping
x: 5
y: 12
after swapping
x: 12
y: 5

your comments are appreciated and if you wants to see your articles on this platform then please shoot a mail at this address kusingh@programmingeeksclub.com

Thanks for reading 🙂

Join Our Newsletter!

Join our newsletter to get our latest ebook "Ultimate JavaScript Cheat-Sheet", and Tips, Articles..

We don’t spam! Read our privacy policy for more info.

Join Our Newsletter!

Join our newsletter to get our latest ebook "Ultimate JavaScript Cheat-Sheet", and Tips, Articles..

We don’t spam! Read our privacy policy for more info.