In this code we will observe how we can swap two numbers. We can perform this by using a temporary variable which is used to store variable so that we can easily swap the numbers.
Code:public class Swapping{ static void swap(int a,int b){ int temp=a; a=b; b=temp; System.out.println("After swapping a = " + a + " b = " + b); } public static void main(String[] args){ int a=1; int b=2; System.out.prinln("Before swapping a="+a+" b="+b); swap(a, b); } }



Reply With Quote
Copyright Techfuels
Bookmarks