Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

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.

<< Back to PROGRAM

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


  • 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.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( );


Enter the value of X and Y
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);
  t = x;
  x = y;
  y = t;
  printf("After swapping: %d    %d",x,y);
  return 0;


Enter the value of X and Y
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))
temp = x
x = y
y = temp
print("After swapping: %d   %d\n"%(x,y))


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

PHP Program

echo "Enter the value of x";
$x = readline();
echo "Enter the value of y";
$y = readline();
echo "before swapping numbers: ";
echo $x;
echo "    ";
echo $y;
echo "\n";
$temp = $x;
$x = $y;
$y = $temp;
echo "After swapping ";
echo $x;
echo "    ";
echo $y;


Enter the value of x
Enter the value of Y
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 x=Convert.ToInt32(Console.ReadLine());
    int y = Convert.ToInt32(Console.ReadLine());
    int temp;
	Console.WriteLine("before swapping numbers: "+x +"	"+ y);
             temp = x;
	   x = y;
	   y = temp;
	Console.WriteLine("After swapping: "+x +"	" + y); 


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

Related Links:

Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf