Coding Challenge Day-19: Problem 1: Swap two numbers without using third variable

Hello! So I’m doing a 30 day coding challenge where I solve a few questions every day and thought of posting them here on my blog so that you guys can join the challenge too!

Welcome to Coding challenge Day 19: Problem 1! Be sure to post your answers, queries etc in the comments!

Problem: Swap two numbers without using third variable

Sample:

Before Swap:
x: 10
y: 20
After Swap:
x: 20
y: 10

Solution:

package misc;

public class Program003swap_numbers_without_third_variable {
	
	public static void main (String [] args){
		int x=10, y=20;
		System.out.println("Before Swap: ");
		System.out.println("x: " +x);
		System.out.println("y: " +y);
		
		x=x+y;
		y=x-y;
		x=x-y;
		System.out.println("After Swap: ");
		System.out.println("x: " +x);
		System.out.println("y: " +y);
	}

}

Download Code

Happy Learning!!

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: