Skip to main content

How Many Different Ways Of Swapping Two Numbers

Well the first thing comes in our mind when we are a beginner is how many ways we can swap two variable but honestly, there are multiple ways of doing it. Generally, for swapping two variables we use a temporary third variable, but there are various other ways where we do not even require the help of any third variable.
So let's see

Method 1:- The simple method using a third variable

1.# method 1
2.a=50
3.b=5
4.temp=a
5.a=b
5.b=temp
6.print(a,b)

Method 2:- Using arithmetic operators + and -

1.#method 2
2.a=50
3.b=5
4.a = a + b
5.b = a - b
6.a = a - b
7.print(a,b)

Method 3:- Using arithmetic operators * and /

1.#method 3
2.a=50
3.b=5
4.a = a * b
5.b = a // b
6.a = a // b
7.print(a,b)

Method 4:- Using bitwise XOR operator ^

1.#method 4
2.a=50
3.b=5
4.a = a ^ b
5.b = a ^ b
6.a = a ^ b
7.print(a,b)

It can also be written in a more compact manner like this:
1.a=50 
2.b=5
3.a ^= b
4.b ^= a
5.a ^= b
6.print(a,b)
Method 5:- The simple method using two variables

1.# method 5
2.a=50
3.b=5
4.a,b=b,a
5.print(a,b)

Comments

Popular posts from this blog

MySQL Multi Source Master Slave Replication using GTID

We are using GTID-based master-slave replication. In this replication, both master and slave should have the same GTID. This blog post aims to provide a step-by-step guide to help you set up MySQL Replication using GTIDs and help you replicate your MySQL data with ease. Prerequisite Master Source 1 Channel name:  DEVELOPMENT Database: mydb1 Enable Replication For Tables testing1 testing2 testing3 Mysql Server Running: 192.168.0.201:3307 Master Source 2 Channel name:  PRODUCTION Database: mydb1 Enable Replication For Tables testing4 testing5 testing6 Mysql Server Running: 192.168.0.201:3308 Slave Mysql Server Running: 192.168.0.209:3306 Configuration of  masters(192.168.0.201:3307/192.168.0.201:3308) and slave (192.168.0.209:3306) my.cnf config for master (192.168.0.201:3307) my.cnf config for master (192.168.0.201:3308) my.cnf config for slave (192.168.0.209:3306) Steps for taking a dump and restoring it Run the following commands in both masters 192.168.0.201:3307 and 1...

Access and modify all the resources of our Wiki.js using WikiJS API

Hi everyone 😎 , Today we are going to see how to access or modify WikiJS resources using WikiJS API. WikiJS exposes GraphQL API to access and modify the stuff according to our requirements. So In this blog post, we going to see  How to set up WikiJS's local environment using docker. How to generate an API token in Wikijs. Using API tokens how to access WikiJS resources and how to modify them. First Local Setup using docker Here I am using docker to set up my local isolated WikiJS environment. Below is my docker-compose.yml file written by WikiJS contributors. To build the WikiJS environment we just need to run the following command  docker-compose up -df  [PATH_OF_DOCKER_COMPOSE_YML_FILE] To check the environment is in a running state run the following command docker ps Second Generate API token After setup our local environment we are ready to create our initial Administrator account in our WikiJS. 1. On the browser type localhost:80 2. Create an Administrator acco...

How to setup an Nginx reverse proxy with a SSL certificate in XWIKI

Hey everyone,  Tonight I continue my previous post of the complete setup of xwiki. This post is about how to setup  Nginx as a reverse proxy with an SSL certificate. Before moving further we consider that xwiki runs on the server( EC2 server) with a domain name called example.in To create a custom domain name using AWS you can follow this link:-  https://aws.amazon.com/getting-started/hands-on/get-a-domain/ By default, Tomcat 9 works on port 8080 , and you can only visit your XWiki site from the same port. If you want to facilitate visitors' access by removing the port number part, you can install Nginx as a reverse proxy between XWiki and visitors. Step 1, Install Nginx using apt: sudo apt install nginx -y Step 2, Setup Nginx as a reverse proxy by modifying its default site configurations: cd /etc/nginx/sites-available  sudo mv default default.bak  sudo vi default Step 3, Edit the file server {  listen 0.0.0.0:80;  proxy_request_buffering off;...