C program to swap two numbers


Posted On: Dec 23, 2020

C program to swap two numbers



Before, Starting this article First off all, I want to introduce you with Number. Then I will explain What is a Swapping? Furthermore, I will demonstrate and write logic for the swaping of two numbers. After all, I will show a C Program to swap two numbers.

Table of contents:

  1. What is a Number?
  2. What is swapping?
  3. Demonstration of Swapping two numbers
  4. Main Logic for Swapping
  5. C program to swap two integers
  6. Conclusion

What is a Number?

Number is an object that uses digits to perform mathematical task. Calculus that is a branch of mathematics included many numbers such as integer, whole number, real number, and imaginary numbers so on. Here, I will discuss about number to perform arithmetic addition operation. Moreover, Number is a combination of digits, some symbols and decimal point. For instance, 127.23 is a rational number.

What is swapping?

Swapping is a mechanism by which you can exchange the value of two variables. For instance, If First number has value 13 and Second number has value 26 Then, swapping stores 26 to First number and 13 to Second number.

Main Logic for Swapping

  • Step 1: Take two variables as num1 and num2 and store some value to it.
  • Step 2: Then, Assign num1 to a temp variable : temp = num1. It becomes num1 free.
  • Step 3: Assign num2 to num1 : num1 = num2. It becomes num2 free.
  • Step 4: After all, Assign temp to num2 : num2 = temp

Demonstration of Swapping two numbers

Demo of Swapping

C program to swap two numbers

// C program to swap two variables 
int main() 
    int num1, num2; 
    printf("Enter Value of num1 "); 
    scanf("%d", &x); 
    printf("\nEnter Value of num2 "); 
    scanf("%d", &y); 
    int temp = num1; 
    num1 = num2; 
    num2 = temp; 
    printf("\nAfter the Successful Swapping: num1 = %d, num2 = %d", num1, num2); 
    return 0; 

The output of the Program:


Output of swapping


This Program stores two integers one by one into the int datatype. After the successful swapping of numbers it will exchange the value of them at once.

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C program to print prime numbers between 1-100

    This Program runs a loop from 2 to 100 and checks for each current number within range that it is prime or not. After the successful execution of the program it displays the list of all prime numbers ..