Contact

Swap Two Numbers in C

In this article, I will explain how to swap two numbers in the C programming language.

Swap Numbers Using Temporary Variable in C

Let's begin by examining a C program that swaps two numbers using a temporary variable.

#include <stdio.h>

int main() {
    double first, second, temp;

    // Prompting the user to enter the first number
    printf("Enter first number: ");
    scanf("%lf", &first);

    // Prompting the user to enter the second number
    printf("Enter second number: ");
    scanf("%lf", &second);

    // Swapping using a temporary variable
    temp = first;
    first = second;
    second = temp;

    // Displaying the swapped numbers with two decimal places
    printf("\nAfter swapping, first number = %.2lf\n", first);
    printf("After swapping, second number = %.2lf", second);

    return 0;
}

This C program prompts the user to enter two numbers, swaps them using a temporary variable, and then displays the swapped numbers formatted to two decimal places. Here is the explanation of the code:

Therefore, the output of this program will be:

Enter first number: 3.5
Enter second number: 7.8

After swapping, first number = 7.80
After swapping, second number = 3.50

Swap Two Numbers Without Using Temporary Variable in C

Now, let's explore another method to swap numbers without using any additional temporary variable.

C Code
#include <stdio.h>

int main() {
    double a, b;

    // Prompting the user to enter value for 'a'
    printf("Enter a: ");
    scanf("%lf", &a);

    // Prompting the user to enter value for 'b'
    printf("Enter b: ");
    scanf("%lf", &b);

    // Swapping without using a temporary variable
    a = a - b;   // 'a' now holds the difference between initial 'a' and 'b'
    b = a + b;   // 'b' now holds the original value of 'a'
    a = b - a;   // 'a' now holds the original value of 'b'

    // Displaying the swapped values with two decimal places
    printf("After swapping, a = %.2lf\n", a);
    printf("After swapping, b = %.2lf", b);

    return 0;
}
Output
Enter a: 4.5
Enter b: 7.8
After swapping, a = 7.80
After swapping, b = 4.50

Here is the explanation of this code:

Swap Two Numbers Using Call by Value in C

In the call by value method, the function receives copies of the values of the variables. Swapping is done within the function using these copies, which does not affect the original variables in the calling function.

C Code
#include <stdio.h>

// Function prototype for swapping using call by value
void swapByValue(double num1, double num2);

int main() {
    double first, second;

    // Prompting the user to enter the first number
    printf("Enter first number: ");
    scanf("%lf", &first);

    // Prompting the user to enter the second number
    printf("Enter second number: ");
    scanf("%lf", &second);

    // Displaying the original values
    printf("\nOriginal values: first = %.2lf, second = %.2lf\n", first, second);

    // Calling the swap function with call by value
    swapByValue(first, second);
	
    // Displaying the swapped values (original values remain unchanged)
    printf("After swapping by value: first = %.2lf, second = %.2lf\n", first, second);

    return 0;
}

// Function definition for swapping using call by value
void swapByValue(double num1, double num2) {
    double temp;

    temp = num1;
    num1 = num2;
    num2 = temp;
    printf("After swapping by value (from inside the function): first = %.2lf, second = %.2lf\n", num1, num2);
}
Output
Enter first number: 3.5
Enter second number: 7.8

Original values: first = 3.50, second = 7.80

After swapping by value (from inside the function): first = 7.80, second = 3.50
After swapping by value: first = 3.50, second = 7.80

The function swapByValue successfully swaps num1 and num2 (inside the function scope) but does not affect the original variables first and second in main due to call by value. Therefore, the final output in main continues to display the original values of first and second. The swapped values (7.80 and 3.50) displayed inside swapByValue are only visible within the function scope and do not impact the original values outside the function.

Swap Two Numbers Using Call by Reference in C

In the call by reference method, the function receives pointers to the memory locations of the variables. Swapping is done using these pointers, allowing modification of the original variables in the calling function.

C Code
#include <stdio.h>

// Function prototype for swapping using call by reference
void swapByReference(double *num1, double *num2);

int main() {
    double first, second;

    // Prompting the user to enter the first number
    printf("Enter first number: ");
    scanf("%lf", &first);

    // Prompting the user to enter the second number
    printf("Enter second number: ");
    scanf("%lf", &second);

    // Displaying the original values
    printf("\nOriginal values: first = %.2lf, second = %.2lf\n", first, second);

    // Calling the swap function with call by reference
    swapByReference(&first, &second);

    // Displaying the swapped values (original values are modified)
    printf("After swapping by reference: first = %.2lf, second = %.2lf\n", first, second);

    return 0;
}

// Function definition for swapping using call by reference
void swapByReference(double *num1, double *num2) {
    double temp;

    temp = *num1;
    *num1 = *num2;
    *num2 = temp;
}
Output
Enter first number: 3.5
Enter second number: 7.8

Original values: first = 3.50, second = 7.80

After swapping by reference: first = 7.80, second = 3.50

That's all for now! I hope you found this article helpful. If you enjoyed it, please consider sharing it with your friends and family. Thank you for reading!




Sharing is stylish! Gift your friends this awesome read!