C Program to Swap Two Numbers

Howdy readers, today we will learn how to write a program to swap two numbers using C Programming language.

C Program to Swap Two Numbers

In this program, we will use a temporary variable to swap two numbers. For example: If a = 5 and b = 7, then after swapping a becomes 7 and b becomes 5.

So, without any delay, let’s begin this tutorial.

C Program to Swap Two Numbers

C Program

// C Program to Swap Two Numbers
#include <stdio.h>
int main(){
    int a, b, temp;
    
    // Asking for input
    printf("Enter the value of a: ");
    scanf("%d", &a);
    printf("Enter the value of b: ");
    scanf("%d", &b);
    
    // Swapping two numbers
    temp = a;
    a = b;
    b = temp;
    
    // Displaying output
    printf("After Swapping:-\n");
    printf("Value of a after swapping: %d \n", a);
    printf("Value of b after swapping: %d", b);
    
    return 0;
}

Output

Enter the value of a: 12
Enter the value of b: 18
After Swapping:-
Value of a after swapping: 18 
Value of b after swapping: 12
C Program to Swap Two Numbers

Explanation

    int a, b, temp;

In this program, we have declared three int data type variables named a, b and temp.

    // Asking for input
    printf("Enter the value of a: ");
    scanf("%d", &a);
    printf("Enter the value of b: ");
    scanf("%d", &b);

Then, the user is asked to enter the two numbers.

    // Swapping two numbers
    temp = a;
    a = b;
    b = temp;

We assign the value of a to temp, and then value of b to a. And at last the value of temp to b.

    // Displaying output
    printf("After Swapping:-\n");
    printf("Value of a after swapping: %d \n", a);
    printf("Value of b after swapping: %d", b);

And then, we print the swapped numbers with the help of printf() function.

C Program to Swap Two Numbers Using Functions

C Program

// C Program to Swap Two Numbers Using Functions
#include <stdio.h>
int swap(int *a, int *b){
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

int main(){
    int num1, num2;
    
    // Asking for input
    printf("Enter the first number: ");
    scanf("%d", &num1);
    printf("Enter the second number: ");
    scanf("%d", &num2);
    
    // Calling out function
    swap(&num1, &num2);
    
    // Display output
    printf("After Swapping:- \n");
    printf("Value of first number after swapping: %d \n", num1);
    printf("Value of second number after swapping: %d", num2);
    
    return 0;
}

Output

Enter the first number: 7
Enter the second number: 12
After Swapping:- 
Value of first number after swapping: 12 
Value of second number after swapping: 7
C Program to Swap Two Numbers Using Functions

Explanation

int swap(int *a, int *b){
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

In this program, we have defined a custom function named swap which swaps the two numbers using a temporary variable.

    // Calling out function
    swap(&num1, &num2);

Then, we call this custom function in the main function. 

Conclusion

I hope after going through this post, you understand how to write a program to swap two numbers using C Programming language.

If you have any difficulty while understanding this program, then let us know in the comment section. We will be delighted to solve your query.

Also Read:

Leave a Comment