C program to swap two numbers using bitwise operator - My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Sunday, November 24, 2019

C program to swap two numbers using bitwise operator - My CS Tutorial

Example of C program to swap two numbers using bitwise operator.


In this tutorial we will learn how to write a C program  that swap two numbers using bitwise operator.





C program code


#include <stdio.h>
#include<conio.h>
void main()
{
    int num1,num2;
    printf("\n Enter num1:\n ");
    scanf("%d",&num1);
    printf("\n Enter num2:\n ");
    scanf("%d",&num2);
    printf("\n Before Swapping Number 1= %d,Number 2= %d\n",num1,num2);
    num1=num1^num2;
    num2=num1^num2;
    num1=num1^num2;
    printf("\n After Swapping Number 1= %d,Number 2= %d\n",num1,num2);
    getch();
}


OUTPUT 1:



OUTPUT 2:



In first output,the value of input first and second number is 45 and 25
After swapping
The value of first number 25 and second number 45

In second output,the value of input first and second number is 50 and 75
After swapping
The value of first number 75 and second number 50


Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.



Created by-- HARSH CHAUHAN

No comments:

Post a Comment