In this page you can see how to swap two numbers using the java programming.
METHOD-I
PROGRAM TO SWAP TWO NUMBERS IN FLOAT:
public class swaptwonumbers{
public static void main(String args[]){
float firstnumber=10.2f,secondnumber=9.7f;
System.out.println("------BEFORE SWAP------");
System.out.println("first number= "+firstnumber);
System.out.println("second number= "+secondnumber);
float temp;
temp=firstnumber;
firstnumber=secondnumber;
secondnumber=temp;
System.out.println("------AFTER SWAP------");
System.out.println("first number = "+firstnumber);
System.out.println("second number ="+secondnumber);
}
}
OUTPUT:
------BEFORE SWAP------
first number= 10.2
second number= 9.7
------AFTER SWAP------
first number = 9.7
second number =10.2
METHOD-II
PROGRAM TO SWAP TWO NUMBERS IN INTEGER:
import java.util.Scanner;
public class swaptwonumbers{
public static void main(String args[]){
int firstnumber,secondnumber;
Scanner sc=new Scanner( System.in);
System.out.println("Enter the first number");
firstnumber=sc.nextInt();
System.out.println("Enter the second number");
secondnumber=sc.nextInt();
System.out.println("------BEFORE SWAP------");
System.out.println("first number= "+firstnumber);
System.out.println("second number= "+secondnumber);
int temp;
temp=firstnumber;
firstnumber=secondnumber;
secondnumber=temp;
System.out.println("------AFTER SWAP------");
System.out.println("first number = "+firstnumber);
System.out.println("second number ="+secondnumber);
}
}
OUTPUT:
Enter the first number
3
Enter the second number
9
------BEFORE SWAP------
first number= 3
second number= 9
------AFTER SWAP------
first number = 9
second number =3
Post a Comment