codescracker
c

C Program to Swap Two Numbers



« Previous Program Next Program »

In this article, you will learn and get code on swapping of two numbers in C language using following approaches:

What is Swapping of Two Numbers ?

Swapping of two numbers means, first number becomes second and second number becomes first. For example, if user enters any two number say 10 and 20. And let's suppose the two variables say num1 and num2 holds these two numbers. That is, num1=10 and num2=20. Then after swapping it will be like num1=20 and num2=10

Using third Variable

Swapping of two numbers in C, becomes easy using third variable. That is, this program uses a variable named temp that helps a lot while performing the swap operation of given two numbers by user at run-time. Let's take a look at the program first

// C Program to Swap Two Numbers
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    int num1, num2, temp;
    printf("Enter Two Numbers:-\n");
    printf("First Number: ");
    scanf("%d", &num1);
    printf("Second Number: ");
    scanf("%d", &num2);
    printf("\nBefore Swap:\n");
    printf("First Number = %d\tSecond Number = %d", num1, num2);
    temp = num1;
    num1 = num2;
    num2 = temp;
    printf("\n\nAfter Swap:\n");
    printf("First Number = %d\tSecond Number = %d", num1, num2);
    getch();
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

c program to swap two numbers

Now enter the first number say 10 and then second number say 20. Press ENTER key to see the following output:

swap two numbers in c

The main block of code for swapping of two numbers is:

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

For example, if user enters 10 as first number, then 10 gets initialized to num1. And if user enters 20 as second number, then 20 gets initialized to num2. That is, num1=10 and num2=20.

Now after executing the statement:

temp = num1;

The value of num1 (that is 10) gets initialized to temp. Therefore, temp=10. And after executing the statement:

num1 = num2;

The value of num2 (that is 20) gets initialized to num1. Therefore, num1=20. And again after executing the statement:

num2 = temp;

The value of temp (that is 10) gets initialized to num2. Therefore, num2=10. In this way, now the variable num1 holds the value that initially num2 has. Whereas the variable num2 holds the value that initially num1 has.

Note - Don't forgot to initialize the value of num1 to any third variable before initializing the value of num2 to it.

Without using third Variable

Unlike the previous program, this program will not use any type of extra (third) variable in neither direct nor in-direct way:

// Swap Two Numbers without using third Variable
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    int num1, num2;
    printf("Enter Two Numbers:-\n");
    printf("First Number: ");
    scanf("%d", &num1);
    printf("Second Number: ");
    scanf("%d", &num2);
    printf("\nBefore Swap:\n");
    printf("First Number = %d\tSecond Number = %d", num1, num2);
    num1 = num1+num2;
    num2 = num1-num2;
    num1 = num1-num2;
    printf("\n\nAfter Swap:\n");
    printf("First Number = %d\tSecond Number = %d", num1, num2);
    getch();
    return 0;
}

This program will produce the same output as of previous one. The following block of code is responsible for swapping of given two numbers. Here I've used the addition and subtraction operation:

num1 = num1+num2;
num2 = num1-num2;
num1 = num1-num2;

For example, let's suppose 10 and 20 are the two numbers entered by user. Therefore. num1=10 and num2=20. Now after executing the following statement:

num1 = num1+num2;

The value comes from num1+num2 (that will be 10+20) gets initialized to num1. Therefore, num1=30. Again after executing the following statement:

num2 = num1-num2;

The value comes from num1-num2 (that will be 30-20) gets initialized to num2. Therefore, num2=10. And again after executing the statement given below:

num1 = num1-num2;

The value comes from num1-num2 (that will be 30-10) gets initialized to num1. Therefore, num1=20. Now the value of first variable goes to second and the value of second variable goes to first one.

The Concept of Using + and - for Swapping

If there are two numbers. Suppose first number becomes total. Then if you subtract second number from total, you'll get the first number. Therefore it gets initialized to num2 (that now holds the initial value of num1). Again if you subtract the new value of second number (initial value of first number) from total, then you will get the initial value of second number. Therefore it gets initialized to num1 (that now holds the initial value of num2).

Using Function and Pointer

This is the last program on swapping of two numbers. This program uses a user=defined function named swapFun() to swap the given two numbers by user:

// Swap Two Numbers using Function and Pointer in C
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
void swapFun(int *, int *);
int main()
{
    int num1, num2;
    printf("Enter Two Numbers:-\n");
    printf("First Number: ");
    scanf("%d", &num1);
    printf("Second Number: ");
    scanf("%d", &num2);
    printf("\nBefore Swap:\n");
    printf("First Number = %d\tSecond Number = %d", num1, num2);
    swapFun(&num1, &num2);
    printf("\n\nAfter Swap:\n");
    printf("First Number = %d\tSecond Number = %d", num1, num2);
    getch();
    return 0;
}
void swapFun(int *num1, int *num2)
{
    int temp;
    temp = *num1;
    *num1 = *num2;
    *num2 = temp;
}

This program also produces the same output as of previous two programs.

In above program, the & is called as address of operator. Whereas the * is called as value at operator. Therefore using the address of operator, we've passed the address of both the variables say num1 and num2 (that holds the two numbers entered by user) to the function named swapFun().

And inside the function swapFun(), we have used the value at operator to fetch the value available at the address of both the variables. The two variables gets swapped inside the function. Because we're using the address of both the variables, therefore the operation that has been done inside the function swapFun() effects the variable's value throughout the program. That is, when we print the value of two variables inside the main() function (after calling the function swapFun()), its value gets swapped.

To learn about Pointers or Functions in detail, then you can follow the separate tutorial on it.

Same Program in Other Languages


« Previous Program Next Program »