# Program to swap two numbers using third variable

Problem statement:- Program to swap two numbers using a third variable.

Data requirement:-

Input Data:-num1, num2

Output Data:-
num1, num2

Program in C

### Here is the source code of the C Program to swap two numbers using a third variable.

Code:

#include<stdio.h>
int main()
{
int num1,num2;
printf("Enter 2 numbers:");
scanf("%d %d",&num1,&num2);
int temp=num1;
num1=num2;
num2=temp;

printf("***After swapping***");
printf("\nNumber 1: %d",num1);
printf("\nNumber 2: %d",num2);
}

Input/Output:
Enter 2 numbers:99
33
***After swapping***
Number 1: 33
Number 2: 99

Program in C++

### Here is the source code of the C++ Program to swap two numbers using a third variable.

Code:

#include<iostream>
using namespace std;
int main()
{
int num1,num2;
cout<<"Enter 2 numbers:";
cin>>num1>>num2;
int temp=num1;
num1=num2;
num2=temp;

cout<<"***After swapping***";
cout<<"\nNumber 1: "<<num1;
cout<<"\nNumber 2: "<<num2;
}

Input/Output:
Enter 2 numbers:
77
111
***After swapping***
Number 1: 111
Number 2: 77

Program in Java

### Here is the source code of the Java Program to swap two numbers using a third variable.

Code:

import java.util.Scanner;
public class SwapTwoNumber {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num1,num2;
System.out.println("Enter 2 numbers:");
num1=cs.nextInt();
num2=cs.nextInt();
int temp=num1;
num1=num2;
num2=temp;

System.out.println("***After swapping***");
System.out.println("Number 1: "+num1);
System.out.println("Number 2: "+num2);
cs.close();
}
}

Input/Output:
Enter 2 numbers:
777
444
***After swapping***
Number 1: 444
Number 2: 777

Program in Python

### Here is the source code of the Python Program to swap two numbers using a third variable.

Code:

num1=int(input("Enter 1st number:"))
num2=int(input("Enter 2nd number:"))

temp=num1
num1=num2
num2=temp

print("***After swapping***")
print("Number 1: ",num1)
print("Number 2: ",num2)

Input/Output:
Enter 1st number:120
Enter 2nd number:300
***After swapping***
Number 1:  300
Number 2:  120