Given below is a Java program to swap two numbers.
Code :
Code :
// Class Swap class Swap{ // method swap void swap(int num1,int num2){ num1 = num1 + num2; num2 = num1 - num2; num1 = num1 - num2; System.out.println("After swapping\nNumber 1= " + num1 + " and Number 2= "+ num2); } } // Class SwapDemo class SwapDemo{ public static void main(String[] args) { int num1=10,num2=20; System.out.println("Number 1: "+num1); System.out.println("Number 2: "+num2); Swap s=new Swap(); s.swap(num1,num2);//call to the swap method } }Output :
Number 1: 10 Number 2: 20 After swapping Number 1= 20 and Number 2= 10Note : Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.
No comments:
Post a Comment