Swap two character without using any variable

 Swap  two character with out using any variable:

public class SwapTwoCharacterWithoutUsingVaraible {
public static void main(String[] args) {
char ch1=’A’;
char  ch2=’d’;
System.out.println(“ch1=”+ch1+”  ch2=”+ch2);
ch1=(char) ((ch1-‘a’)+(ch2-‘a’));
ch2= (char) ((‘a’)+ch1-(ch2-‘a’));
ch1= (char) (‘a’+ch1-(ch2-‘a’));
System.out.println(“after swap”);
System.out.println(“ch1=”+ch1+”   ch2=”+ch2);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

+ 48 = 54