Swapping Two Numbers Program

CCBeginner
Practice Now

Introduction

Swapping two numbers means interchanging their values. In this lab, we will learn how to swap two numbers in C language using different methods such as using a temporary variable, addition and subtraction, bitwise operators, multiplication and division.

Note: You need to create the file ~/project/main.c yourself to practice coding and learn how to compile and run it using gcc.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123348{{"`Swapping Two Numbers Program`"}} c/comments -.-> lab-123348{{"`Swapping Two Numbers Program`"}} c/variables -.-> lab-123348{{"`Swapping Two Numbers Program`"}} c/data_types -.-> lab-123348{{"`Swapping Two Numbers Program`"}} c/operators -.-> lab-123348{{"`Swapping Two Numbers Program`"}} c/pointers -.-> lab-123348{{"`Swapping Two Numbers Program`"}} c/function_declaration -.-> lab-123348{{"`Swapping Two Numbers Program`"}} end

Using a Temporary Variable

We can swap two numbers using a temporary variable by following these steps:

  1. Declare three variables x, y and temp.
  2. Assign values to x and y.
  3. Store the value of x in temp.
  4. Assign the value of y to x.
  5. Assign the value of temp to y.
  6. Print the values of x and y.
#include <stdio.h>

int main() {
  int x = 5, y = 7, temp;

  // Step 3
  temp = x;

  // Step 4 and 5
  x = y;
  y = temp;

  // Step 6
  printf("After swapping, x = %d and y = %d\n", x, y);

  return 0;
}

Using Addition and Subtraction

We can swap two numbers using addition and subtraction by following these steps:

  1. Assign values to x and y.
  2. Add x and y and assign the result to x.
  3. Subtract the original value of y from x and assign to y.
  4. Subtract the original value of y from the new value of x and assign to x.
  5. Print the values of x and y.
#include <stdio.h>

int main() {
  int x = 5, y = 7;

  // Step 2 and 3
  x = x + y;
  y = x - y;

  // Step 4
  x = x - y;

  // Step 5
  printf("After swapping, x = %d and y = %d\n", x, y);

  return 0;
}

Using Bitwise Operators

We can swap two numbers using bitwise operators by following these steps:

  1. Assign values to x and y.
  2. XOR x and y and assign the result to x.
  3. XOR the new value of x and y and assign the result to y.
  4. XOR the new value of x and y and assign the result to x.
  5. Print the values of x and y.
#include <stdio.h>

int main() {
  int x = 5, y = 7;

  // Step 2 and 3
  x = x ^ y;
  y = x ^ y;

  // Step 4
  x = x ^ y;

  // Step 5
  printf("After swapping, x = %d and y = %d\n", x, y);

  return 0;
}

Using Multiplication and Division

We can swap two numbers using multiplication and division by following these steps:

  1. Assign values to x and y.
  2. Multiply x and y and assign the result to x.
  3. Divide the new value of x by y and assign the result to y.
  4. Divide the new value of x by the new value of y and assign the result to x.
  5. Print the values of x and y.
#include <stdio.h>

int main() {
  int x = 5, y = 7;

  // Step 2 and 3
  x = x * y;
  y = x / y;

  // Step 4
  x = x / y;

  // Step 5
  printf("After swapping, x = %d and y = %d\n", x, y);

  return 0;
}

Summary

In this lab, we learned how to swap two numbers using four different methods: using a temporary variable, addition and subtraction, bitwise operators and multiplication and division. Swapping two numbers is useful in programming when we want to change the order of two values or perform operations that require switching the values of two variables.

Other C Tutorials you may like