Swap two numbers using bitwise operators

 

Problem Statement:- Program to swap two numbers using bitwise operators.

Sample Input/Output:-


Sample Input First:

17

21

Sample Output First: 

21

17

Sample Input Second: 

33

72

Sample Output Second: 

72

33


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 bitwise operators.


Code:

#include <stdio.h>
int main()
{
    int num1num2;
    /* Get the two numbers input */
    printf("Enter the two Numbers:");
    scanf("%d %d", &num1, &num2);
    /*Swaping two numbers using bitwise XOR operators.*/
    num1 = num1 ^ num2/* ^(XOR operators) */
    num2 = num1 ^ num2;
    num1 = num1 ^ num2;
    printf("After Swapping two Numbers are:\n");
    printf("Number1=%d\nNumber2=%d\n"num1num2);

    return 0;
}


Input/Output:

Enter the two Numbers:17 21

After Swapping two Numbers are:

Number1=21

Number2=17


Program in C++

Here is the source code of the C++ Program to swap two numbers using bitwise operators.


Code:

#include <iostream>
using namespace std;
int main()
{
    int num1num2;
    /* Get the two numbers input */
    cout << "Enter the two Numbers:";
    cin >> num1 >> num2;
    /*Swaping two numbers using bitwise XOR operators.*/
    num1 = num1 ^ num2; /* ^(XOR operators) */
    num2 = num1 ^ num2;
    num1 = num1 ^ num2;
    cout << "After Swapping two Numbers are:\n";
    cout << "Number1= " << num1 << "\nNumber2= " << num2;
    return 0;
}


Input/Output:

Enter the two Numbers:33 72

After Swapping two Numbers are:

Number1= 72

Number2= 33


Program in Java

Here is the source code of the Java Program to swap two numbers using bitwise operators.


Code:

import java.util.Scanner;

public class SwapTwoNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        /* Get the two numbers input */
        System.out.print("Enter the Two Numbers:");
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();
        /* Swaping two numbers using bitwise XOR operators. */
        num1 = num1 ^ num2/* ^(XOR operators) */
        num2 = num1 ^ num2;
        num1 = num1 ^ num2;
        System.out.print("After Swapping two Numbers are:\nNumber1= " + num1 + "\nNumber2= " + num2);
        sc.close();
    }
}


Input/Output:

Enter the Two Numbers:51 99

After Swapping two Numbers are:

Number1= 99

Number2= 51


Program in Python

Here is the source code of the Python Program to swap two numbers using bitwise operators.


Code:

#  Get the two numbers input
num1num2map(intinput("Enter the two Numbers:").split())
#Swaping two numbers using bitwise XOR operators.
num1 = num1 ^ num2 #^(XOR operators)
num2 = num1 ^ num2
num1 = num1 ^ num2
print("After Swapping two Numbers are:","\nNumber1= ",num1,"\nNumber2= ",num2)


Input/Output:

Enter the two Numbers:145 368

After Swapping two Numbers are: 

Number1=  368

Number2=  145

Most Recommend Questions:-







More Questions:-


Post a Comment

0 Comments