C program to swap two numbers-Swapping of two integers in C language


 C Program to Swap Two Numbers


Today in this post, we will learn how to swap two numbers in c programming. To swap two numbers we will perform this operation by taking a temporary variable (call temp) apart from two variables (num1 and num2) which contains the numbers. To swap two numbers first we will store the value of num1  in temporary variable and the store the value of num2 in num1 and then finally we will store the value of num1 which we had store in temp to num2 and we will get the number swapped.

Algorithm:-

  1. Start.
  2. Input number one from user.
  3. Input number two from user.
  4. Declare a temporary variable to swap two numbers.
  5. Store num1 in temp.
  6. Then store num2 in num1.
  7. And then finally store temp in num2.
  8. Stop.

How Swapping works with temporary variable:-



temp = num1;
num1 = num2;
num2 = temp;
Look at the code snippet given above to understand how swapping works here. First we create a temp variable which is a temporary variable to store the values. First we store the value of num1 variable in the temp variable and then store the value of num2 variable in num1, so now num1 contains the value of num2 and then finally store the value of temp which contains the value num1 into num2. Now the values of both the variables num1 and num2 are swapped.

Lets consider an example:-


//after swapping 
num1 = 5;
num2 = 6;

temp = num1;
num1 = num2;
num2 = temp;

//after swapping 
num1 = 6;
num2 = 5;
Here, in the example given above before swapping variable num1 contains the value 5 and variable num2 contains the value 6. First we store the value of num1(i.e 5) in temp. Now temp contains 5 and then we store the value of num2(i.e 6) variable in num1, so num1 contains 6 now and then we store the value of variable temp which is 5 in num2, so now num2 contains 6. As you can see we have swapped the two numbers with the help of temp variable.

Consider another example:-


//after swapping 
num1 = 103;
num2 = 67;

temp = num1;
num1 = num2;
num2 = temp;

//after swapping 
num1 = 67;
num2 = 103;
Learn how to swap two numbers without using temporary variable here.

Program:-



#include <stdio.h>

int main()
{
    int num1, num2, temp;
    printf("Enter num1: ");
    scanf("%d", &num1);
    printf("Enter num2: ");
    scanf("%d", &num2);
    
    printf("Before swapping: num1 = %d and num2 = %d", num1, num2);
    
    temp = num1;
    num1 = num2;
    num2 = temp;
    
    printf("\nAfter swapping: num1 = %d and num2 = %d", num1, num2);


    return 0;
}


Output:-

Enter num1: 999                                                                                                       
Enter num2: 1000                                                                                                      
Before swapping: num1 = 999 and num2 = 1000                                                                           
After swapping: num1 = 1000 and num2 = 999 


Enter num1: 23                                                                                                        
Enter num2: 12                                                                                                        
Before swapping: num1 = 23 and num2 = 12                                                                              
After swapping: num1 = 12 and num2 = 23

Post a Comment

0 Comments