# Program to swap two numbers

Program to swap two numbers on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.

## Program to swap two numbers

This program is to swap/exchange two numbers by using a variable.

For example:

Numbers to swap: 11 and 12
Let x= 11, y= 12

Swapping Logic:
t=x= 11
x =y =12
y =t =11

After swapping:
x= 12, y = 11

### Algorithm

• STEP 1: START
• STEP 2: DEFINE x, y, t
• STEP 3: ENTER x, y
• STEP 4: PRINT x, y
• STEP 5: t = x
• STEP 6: x= y
• STEP 7: y= t
• STEP 8: PRINT x, y
• STEP 9: END

### Java Program

import java.util.*;
class Swap_With {
public static void main(String[] args) {
int x, y, t;// x and y are to swap
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of X and Y");
x = sc.nextInt();
y = sc.nextInt();
System.out.println("before swapping numbers: "+x +"	"+ y);
/*swapping */
t = x;
x = y;
y = t;
System.out.println("After swapping: "+x +"	" + y);
System.out.println( );
}
}

Output:

Enter the value of X and Y
2
3
before swapping numbers: 2   3
After swapping: 3   2

### C Program

#include <stdio.h>
int main()
{
int x, y, t;// x and y are the swapping variables and t is another variable
printf("Enter the value of X and Y\n");
scanf("%d%d", &x, &y);
printf("before swapping numbers: %d   %d\n",x,y);
/*swapping*/
t = x;
x = y;
y = t;
printf("After swapping: %d    %d",x,y);
return 0;
}

Output:

Enter the value of X and Y
29
39
before swapping numbers: 29   39
After swapping: 39  29

### Python Program

x = int(input("Enter the value of x?"))
y = int(input("Enter the value of y?"))
print("before swapping numbers: %d   %d\n" %(x,y))
#swapping#
temp = x
x = y
y = temp
print("After swapping: %d   %d\n"%(x,y))

Output:

Enter the value of X?  87
Enter the value of Y?  43
before swapping numbers: 87  43
After swapping: 43  87

### PHP Program

<?php
echo "Enter the value of x";
echo "Enter the value of y";
echo "before swapping numbers: ";
echo \$x;
echo "    ";
echo \$y;
echo "\n";
/*swapping*/
\$temp = \$x;
\$x = \$y;
\$y = \$temp;
echo "After swapping ";
echo \$x;
echo "    ";
echo \$y;
?>

Output:

Enter the value of x
33
Enter the value of Y
22
before swapping numbers: 33  22
After swapping  22  33

### C# Program

using System;
public class Swap
{
public static void Main()
{
Console.WriteLine("Enter the value of x and y");
int temp;
Console.WriteLine("before swapping numbers: "+x +"	"+ y);
/*swapping*/
temp = x;
x = y;
y = temp;
Console.WriteLine("After swapping: "+x +"	" + y);
}}

Output:

Enter the value of x and y
65
54
before swapping numbers:  65  54
After swapping:  54  65
Next Topic#