Java program for swapping of 2 numbers using method

Java method for swapping of 2 number

simple example for java method

Program:-

import java.util.Scanner;

class group{
public static void main(String ang[]){
Scanner data= new Scanner(System.in);

int num1,num2;
//Reading value
System.out.println("Enter num1:");
num1=data.nextInt();
System.out.println("Enter num2:");
num2=data.nextInt();

//Passing value of num1 & num2 to swap method
swap(num1,num2);

}

// swap method
public static void swap(int n1, int n2)
{
int temp;

temp=n1;
n1=n2;
n2=temp;

       //displaying output
System.out.println("num1: "+ n1 +" num2: "+ n2);
}
}


Output:-

Enter num1:
58
Enter num2:
69
num1: 69 num2: 58

Leave reply

Add your comments here

Back to Top