Swap 2 Numbers Using Macros: C Program

Today lets learn how to swap two integer numbers(using a temporary variable) using Macros in C.

Video Tutorial: Swap 2 Numbers Using Macros: C Program



YouTube Link: https://www.youtube.com/watch?v=esQ-DmyPYYc [Watch the Video In Full Screen.]

Source Code: Swap 2 Numbers Using Macros: C Program

  1. #include<stdio.h>  
  2.   
  3. #define SWAP(x, y, temp) temp = x; x = y; y = temp;  
  4.   
  5. int main()  
  6. {  
  7.     int a, b, temp;  
  8.   
  9.     printf("Enter 2 integer numbers\n");  
  10.     scanf("%d%d", &a, &b);  
  11.   
  12.     printf("Before swapping: a = %d and b = %d\n", a, b);  
  13.   
  14.     SWAP(a, b, temp);  
  15.   
  16.     printf("After swapping: a = %d and b = %d\n", a, b);  
  17.   
  18.     return 0;  
  19. }  

Output:
Enter 2 integer numbers
20
50
Before swapping: a = 20 and b = 50
After swapping: a = 50 and b = 20

Logic To Swap Two Numbers

First value of a is transferred to temp;
Next value of b is transferred to a.
Next value of temp is transferred to b.



That’s how value of a and b are swapped using a temporary variable.

Note: Preprocessor replaces the macro template(SWAP(a, b, temp)) with its corresponding macro expansion(temp = x; x = y; y = temp;) before passing the source code to the compiler.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Swap 2 numbers using Multiplication and Division: C

In this video tutorial we shall learn how to swap two integer numbers without using a temporary variable and by simply making use of multiplication and division.

Related Read:
Basic Arithmetic Operations In C
Swap 2 Numbers Using a Temporary Variable: C
Swap 2 Numbers Without Using a Temporary Variable: C
Swap 2 numbers using Addition and Subtraction: C

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a, b;  
  7.   
  8.     printf("Enter 2 integer numbers\n");  
  9.     scanf("%d %d", &a, &b);  
  10.   
  11.     printf("You entered a = %d and b = %d\n", a, b);  
  12.   
  13.     a = a * b;  
  14.     b = a / b;  
  15.     a = a / b;  
  16.   
  17.     printf("After swapping a = %d and b = %d\n", a ,b);  
  18.   
  19.     return 0;  
  20. }  

Output:
Enter 2 integer numbers
3
5
You entered a = 3 and b = 5
After swapping a = 5 and b = 3

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Swap 2 numbers using only Multiplication and Division: C



YouTube Link: https://www.youtube.com/watch?v=AcMEvVD0D4U [Watch the Video In Full Screen.]


Swapping 2 Numbers In C using only Multiplication and Division: Logic

In our above program we are asking user to enter integer value for a and b.
If the user enters 10 and 5 for a and b respectively. Then our program executes below logic to swap the values of variable a and b.

Step 1: Multiply values of a and b and store it in variable a.
i.e.,
a = a * b;
50= 10 * 5;
So a = 50;

Step 2: Now divide value of b from value of a and store it in variable b.
i.e.,
b = a / b;
10= 50 / 5; (value of a = 50 according to Step 1)
So value of variable b is now 10.

Step 3: Now divide value of b from value of a and store it in variable a.
i.e.,
a = a / b;
5= 50 / 10; (Value of a = 50 from Step 1, and Value of b = 10 from Step 2)
Now the value of a = 5;

Finally value of a = 5(from step 3) and value of b = 10(from step 2).

This is how we swap the value of variable a and b by just making use of multiplication and division in C programming language.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Swap 2 numbers using Addition and Subtraction: C

In this video tutorial we shall learn how to swap two integer numbers without using a temporary variable and by simply making use of addition and subtraction.

Related Read:
Basic Arithmetic Operations In C
Swap 2 Numbers Using a Temporary Variable: C
Swap 2 Numbers Without Using a Temporary Variable: C

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a, b;  
  7.   
  8.     printf("Enter 2 integer numbers for, a and b\n");  
  9.     scanf("%d %d", &a, &b);  
  10.   
  11.     printf("You entered a = %d and b = %d\n", a ,b);  
  12.   
  13.     a = a + b;  
  14.     b = a - b;  
  15.     a = a - b;  
  16.   
  17.     printf("After swapping a = %d, b = %d\n", a, b);  
  18.   
  19.     return 0;  
  20. }  

Output:
Enter 2 integer numbers, for a and b
30
20
You entered a = 30 and b = 20
After swapping a = 20, b = 30

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Swap 2 numbers using only Addition and Subtraction: C



YouTube Link: https://www.youtube.com/watch?v=mm4XJ2DEVS4 [Watch the Video In Full Screen.]


Swapping 2 Numbers In C using only addition and subtraction: Logic

In our above program we are asking user to enter integer value for a and b.
If the user enters 30 and 20 for a and b respectively. Then our program executes below logic to swap the values of variable a and b.

Step 1: Add values of a and b and store it in variable a.
i.e.,
a = a + b;
50 = 30 + 20;
So a = 50;

Step 2: Now subtract value of b from value of a and store it in variable b.
i.e.,
b = a – b;
30= 50 – 20; (value of a = 50 according to Step 1)
So value of variable b is now 30.

Step 3: Now subtract value of b from value of a and store it in variable a.
i.e.,
a = a – b;
20= 50 – 30; (Value of a = 50 from Step 1, and Value of b = 30 from Step 2)

Finally value of a = 20(from step 3) and value of b = 30(from step 2).

This is how we swap the value of variable a and b by just making use of addition and subtraction in C programming language.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Swap 2 Numbers Without Using a Temporary Variable: C

Today lets learn how to swap 2 integer numbers without using a temporary variable in C. We’re using arithmetic operation in our logic. We’re using addition and subtraction in this c program to swap the 2 numbers.

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int x = 10, y = 5;  
  7.   
  8.     printf("Before swapping x = %d and y = %d\n", x, y);  
  9.   
  10.     x = x + y - ( y = x );  
  11.   
  12.     printf("After swapping x = %d and y = %d\n", x, y);  
  13.   
  14.     return 0;  
  15. }  

Output:
Before swapping x = 10 and y = 5
After swapping x = 5 and y = 10

Swap 2 Numbers Without Using a Temporary Variable: C



YouTube Link: https://www.youtube.com/watch?v=tTzdGK5t-X4 [Watch the Video In Full Screen.]


Swapping 2 Numbers In C using addition, subtraction and assignment operators: Logic

Here we take 2 variables, x and y; We store 10 in x and 5 in y.

We add the values of x(10) and y(5) and then subtract the value returned when we assign x value to y.

Note: When we assign a value to a variable, the same value is returned.
Ex: temp = (a = 10);
In this statement, when we assign 10 to a, the whole expression returns 10 and we can store it in variable temp, for our reference.

Swap 2 Numbers Using a Temporary Variable: C

Today lets learn how to swap 2 integer numbers using a temporary variable in C.

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int x = 10, y = 20, temp;  
  7.   
  8.     printf("X = %d, Y = %d\n", x, y);  
  9.       
  10.     temp = x;  
  11.     x    = y;  
  12.     y    = temp;  
  13.   
  14.     printf("After swapping X = %d, Y = %d\n", x, y);  
  15.   
  16.     return 0;  
  17. }  

Output:
X = 10, Y = 20
After swapping X = 20, Y = 10

Swap 2 Numbers Using a Temporary Variable: C



YouTube Link: https://www.youtube.com/watch?v=f_JBCYWS2uw [Watch the Video In Full Screen.]


Swapping 2 Numbers In C: Logic

Here we take 3 variables, x, y and temp; We store 10 in x and 20 in y.
1. First we copy the value present in x to temp. So now, both x and temp variables have value 10.
2. Next we copy value of y to x. So now, both x and y have value 20.
3. Next, copy the value of temp to y. So now both y and temp have value 10.

Finally after executing above 3 step logic, x has value 20 and y has a value of 10 in it.