Dremendo Tag Line

Swap numbers using third variable in C

scanf() - Question 9

In this question, we will see how to input two integers into two variables x and y in C programming using the scanf() function and swap their values using a third variable z. To know more about scanf() function click on the scanf() function lesson.

Q9) Write a program in C to input two integers into two variables x and y and swap their values using a third variable z.

x = 5
y = 3

After Swap
x = 3
y = 5

Program

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

int main()
{
    int x,y,z;
    printf("Enter value of x ");
    scanf("%d",&x);
    printf("Enter value of y ");
    scanf("%d",&y);
    z=x;
    x=y;
    y=z;
    printf("After Swap\n");
    printf("x=%d\n",x);
    printf("y=%d",y);
    return 0;
}

Output

Enter value of x 10
Enter value of y 20
After Swap
x=20
y=10
video-poster