C program to swap using third variable

c program to swap two numbers using third variable : Hello! This c program is very important as it is mostly taught and asked in external, vivas and in campus recruitment exams also.Swap two numbers means interchanging the value of one variable to another. We are Swapping the value of variable ‘a’ and ‘b’ by using a third variable ‘temp’. While by using some tricks you can make c program to swap two numbers without using third variable. Lets look on this c program to swap two numbers using third variable. If you like to know about call by reference and call by value, see the examples of swapping that i have taken i.e as c program to swap two numbers using call by value and using call by reference.

C program to swap

#include<stdio.h>
#include<conio.h>

int main()
{
    int a,b,temp;
    printf("enter two values\n"); 
    printf("\na = ");
    scanf("%d",&a);
    printf("\nb = ");
    scanf("%d",&b);
    temp=a;
    a=b;
    b=temp;
    printf("\na = %d\tb = %d ", a, b);
    getch();
}

Output of C program to swap:

C program to swap
C program to swap

4 thoughts on “C program to swap using third variable”

      1. Here you go

        As you know that
        7 multiply by 4 is equal to 28
        and
        4 multiply by 7 is also equal to 28

        We can use the similar approach to swap values of variables

        For Example like this

        x=3
        y=2

        x=y*x; //x=6
        y=x/y; //y=3
        x=x/y; //x=2

        I hope it was helpful for you :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>