The document provides a Java program that demonstrates how to swap two numbers using a third variable. It outlines the procedure step-by-step and includes the complete code for the swap operation, which results in the output of swapped values. Additionally, it mentions a task to write a Java program that reads input from the end-user.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
8 views2 pages
MODULE - 6 Java
The document provides a Java program that demonstrates how to swap two numbers using a third variable. It outlines the procedure step-by-step and includes the complete code for the swap operation, which results in the output of swapped values. Additionally, it mentions a task to write a Java program that reads input from the end-user.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2
MODULE – 6
JAVA PROGRAMMING
1. Write a Java program to swap two numbers using third variable?
Procedure:- 1) Take two numbers. For example:- int x = 10; int y = 20 2) declare a temporary/third variable of same data type, int temp; 3) Assign x value to third variable, temp = x; 4) Now, assign y value to x variable, x = y 5) Finally, assign temp value to y variable, y = temp; int x = 10, y = 20; int temp = x; x = y; y = x; Execution:- x = 10, y = 20 temp = x => temp = 10 (temp is hold value of x) x = y=> x = 20 ( now, x is holding value of y) y = temp => y = 10 (now, y is holding previous value of x) public class Swap {
public static void main(String[] args) {
int x=10, y=20;
int temp;
temp = x; x = y; y = temp;
System.out.println("Values After Swapping,");
System.out.println("x="+x+"\t y="+y); } } Output:- Values After Swapping, x=20 y=10
TASKS TO BE COMPLETED:
1. Write a Java program through reading input from the end-user?