C Program to Swap Two Numbers

1. Introduction

Swapping is a fundamental operation in many computer algorithms, especially sorting. By 'swap', we mean exchanging the values of two variables. This post will dive into a C program that swaps the values of two numbers, demonstrating both the standard approach and a neat mathematical trick to achieve this.

2. Program Overview

The steps our program will follow are:

1. Take two numbers from the user.

2. Swap the numbers.

3. Display the numbers after swapping.

3. Code Program

#include <stdio.h>  // Bring in the Standard I/O library for input-output functions

int main() {  // The main function begins

    double num1, num2, temp;  // Declare two numbers and a temporary variable to assist in swapping

    // Get the user's numbers
    printf("Enter first number: ");
    scanf("%lf", &num1);
    printf("Enter second number: ");
    scanf("%lf", &num2);

    // Using a third variable to swap numbers
    temp = num1;  // Store the initial value of num1
    num1 = num2;  // Assign the value of num2 to num1
    num2 = temp;  // Assign the stored value of num1 to num2

    printf("After swapping, first number = %.2lf\n", num1);
    printf("After swapping, second number = %.2lf\n", num2);

    return 0;  // Indicate successful termination of the program
}

Output:

Enter first number: 5.5
Enter second number: 6.6
After swapping, first number = 6.60
After swapping, second number = 5.50

4. Step By Step Explanation

1. #include <stdio.h>: By including this header, we can use standard input and output functions.

2. int main(): Every C program starts from the main function.

3. Variable Declaration:

- We're using the double type to handle potential decimal numbers.

- temp is a temporary variable to help in the swapping process.

4. User Input: The program asks the user for two numbers and stores them in num1 and num2.

5. The Swap:

- The value of num1 is stored in temp.

- Then, the value of num2 is assigned to num1.

- Finally, the original value of num1 (stored in temp) is assigned to num2. Thus, completing the swap.

6. Output: The swapped values are then displayed to the user.

This traditional method of using a third variable to swap values is straightforward and easy to understand. 

5. C Program to Swap Two Numbers without Temp Variable

#include <stdio.h>

int main() {
    int a, b;

    // Reading input from the user
    printf("Enter first number: ");
    scanf("%d", &a);

    printf("Enter second number: ");
    scanf("%d", &b);

    // Displaying numbers before swapping
    printf("Before swapping: a = %d, b = %d\n", a, b);

    // Swapping without using a temp variable
    a = a + b;
    b = a - b; // (a+b) - b = a
    a = a - b; // (a+b) - a = b

    // Displaying numbers after swapping
    printf("After swapping: a = %d, b = %d\n", a, b);

    return 0;
}

Output:

Enter first number: 10
Enter second number: 20
Before swapping: a = 10, b = 20
After swapping: a = 20, b = 10
The logic behind swapping without a temporary variable is based on simple arithmetic. By adding both numbers and then subtracting each from the sum, you can effectively swap their values. It's a neat trick that can save a little bit of memory, but one should be cautious of potential overflow if the numbers are large.

Comments