Write a  Java Program to Swap Two Numbers

Java Program to Swap Two Numbers

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to Swap Two Numbers
Java Program to Swap Two Numbers

Write a  Java Program to Swap Two Numbers

PROGRAM: Java Program to Swap Two Numbers

/* Java Program to Swap Two Numbers */

public class SwapNumbers {

public static void main(String[] args) {

float first = 1.20f, second = 2.45f;

System.out.println(“–Before swap–“);
System.out.println(“First number = ” + first);
System.out.println(“Second number = ” + second);

// Value of first is assigned to temporary
float temporary = first;

// Value of second is assigned to first
first = second;

// Value of temporary (which contains the initial value of first) is assigned to second
second = temporary;

System.out.println(“–After swap–“);
System.out.println(“First number = ” + first);
System.out.println(“Second number = ” + second);
}
}

 

 

Leave a Reply

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