Carbon Program to swap digits

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

Using a temp variable.
Without using a temp variable.

1. Using a temp variable

The idea is simple for this approach for swapping tw…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Kuldeep Singh

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

  1. Using a temp variable.
  2. Without using a temp variable.

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

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

  • Assign to y the sum of x and b i.e. y = x + y.
  • Assign to x difference of y and x i.e. x = y – x.
  • 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

Learn More

You can find more content like this on programmingeeksclub.com


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Kuldeep Singh


Print Share Comment Cite Upload Translate Updates
APA

Kuldeep Singh | Sciencx (2022-10-29T13:11:41+00:00) Carbon Program to swap digits. Retrieved from https://www.scien.cx/2022/10/29/carbon-program-to-swap-digits/

MLA
" » Carbon Program to swap digits." Kuldeep Singh | Sciencx - Saturday October 29, 2022, https://www.scien.cx/2022/10/29/carbon-program-to-swap-digits/
HARVARD
Kuldeep Singh | Sciencx Saturday October 29, 2022 » Carbon Program to swap digits., viewed ,<https://www.scien.cx/2022/10/29/carbon-program-to-swap-digits/>
VANCOUVER
Kuldeep Singh | Sciencx - » Carbon Program to swap digits. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/29/carbon-program-to-swap-digits/
CHICAGO
" » Carbon Program to swap digits." Kuldeep Singh | Sciencx - Accessed . https://www.scien.cx/2022/10/29/carbon-program-to-swap-digits/
IEEE
" » Carbon Program to swap digits." Kuldeep Singh | Sciencx [Online]. Available: https://www.scien.cx/2022/10/29/carbon-program-to-swap-digits/. [Accessed: ]
rf:citation
» Carbon Program to swap digits | Kuldeep Singh | Sciencx | https://www.scien.cx/2022/10/29/carbon-program-to-swap-digits/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.