# 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.712803,"math_prob":0.9914543,"size":3149,"snap":"2023-40-2023-50","text_gpt3_token_len":795,"char_repetition_ratio":0.14848967,"word_repetition_ratio":0.03649635,"special_character_ratio":0.28231186,"punctuation_ratio":0.18068536,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99618495,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-27T10:59:55Z\",\"WARC-Record-ID\":\"<urn:uuid:19cc12a7-878f-4cc2-9c6d-760d69814427>\",\"Content-Length\":\"170106\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c428b50d-adc3-4231-816a-43a8762562a1>\",\"WARC-Concurrent-To\":\"<urn:uuid:4befa0ec-4218-4932-865e-762514a77bb1>\",\"WARC-IP-Address\":\"142.251.16.121\",\"WARC-Target-URI\":\"https://www.javaguides.net/2023/09/c-program-to-swap-two-numbers.html\",\"WARC-Payload-Digest\":\"sha1:5T5HY4W3AIHX6MGNXQAAFLETFWV57JAI\",\"WARC-Block-Digest\":\"sha1:5OSSMXRCJOJ2PF2AYLMN26WKA4PK6EMN\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510297.25_warc_CC-MAIN-20230927103312-20230927133312-00037.warc.gz\"}"}