C Program to Swap Two Numbers

CodingTute

Updated on:

C Program to Swap Two Numbers

In this example, you will learn how to swap two numbers with two different methods.

Method 1: Swap Two Numbers using Temporary variable

Method 2: Swap Two Numbers without using Temporary variable

C Program to Swap Two Numbers using temporary variable

#include <stdio.h>

int main()
{
    int a, b, temp;
    printf("Enter A value \n");
    scanf("%d",&a);
    printf("Enter B value \n");
    scanf("%d",&b);
    
    printf("Value of A : %d\n",a);
    printf("Value of B : %d\n",b);
    
    //swap logic
    temp=a;
    a=b;
    b=temp;
    
    printf("Value of A after swap: %d\n",a);
    printf("Value of B after swap: %d\n",b);
    
    return 0;
}
Run Code on Online C Compiler

Output

Enter A value 10
Enter B value 20
Value of A : 10
Value of B : 20
Value of A after swap: 20
Value of B after swap: 10

Explanation

This C program asks the user to enter two integer values a and b, reads them using scanf(), and then swaps the values of a and b using a temporary variable temp. The program then displays the values of a and b before and after the swap using printf().

The program first declares three integer variables a, b, and temp. It then prints a message to the console asking the user to enter the value of a using printf(). The scanf() function is used to read the value of a entered by the user, and the address of a is passed as the second argument to scanf() using the & operator to ensure that the value entered by the user is stored in the memory location reserved for the a variable.

Similarly, the program prompts the user to enter the value of b using printf(), and reads it using scanf() with the address of b passed as the second argument.

Next, the program displays the values of a and b before the swap using printf(). Then, it performs the swap by storing the value of a in temp, assigning the value of b to a, and finally assigning the value of temp to b.

Finally, the program displays the values of a and b after the swap using printf(), and returns 0 to indicate successful completion of the main() function.

C Program to Swap two numbers without using Temporary variable

#include <stdio.h>
int main() {
  int a, b;
  printf("Enter A: \n");
  scanf("%d", &a);
  printf("Enter B: \n");
  scanf("%d", &b);

  // swapping logic

  // a = (a - b)
  a = a - b;   

  // b = (a - b) + b = a (initial)
  b = a + b;

  // a = a - (a - b) = b (initial)
  a = b - a;

  printf("Value of A after swap: %d\n", a);
  printf("Value of B after swap: %d\n", b);

  return 0;
}
Run Code on Online C Compiler

Output:

Enter A: 10
Enter B: 29
Value of A after swap: 29
Value of B after swap: 10

Explanation

This C program prompts the user to enter two integer values a and b, reads them using scanf(), swaps their values without using a temporary variable, and then displays the swapped values of a and b using printf().

After declaring a and b as integer variables, the program asks the user to input the values of a and b using printf() and scanf(), respectively.

Next, the program performs the swap operation without using a temporary variable. It subtracts b from a and assigns the result to a, which effectively stores the difference between the two variables in a. Then, it adds b to the new value of a and assigns the result to b, which effectively stores the original value of a in b. Finally, it subtracts the new value of a from b and assigns the result to a, which effectively stores the original value of b in a. At this point, the values of a and b have been swapped.

Lastly, the program displays the values of a and b after the swap using printf(), and returns 0 to indicate successful completion of the main() function.

Overall, this program demonstrates a way to swap two variables without using a temporary variable in C programming language.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.