Java program to swap two values without using third variable
Answers
Answered by
7
Lets open the notepad and write the following code:
Lets try to find the error in this program. if we write the small 's' on the place of capital 'S'.
class demo
{
public static void main(string arg[])
{
System.out.println("Before swapping");
int x=10;
int y=20;
System.out.println("value of x:"+x);
System.out.println("value of y:"+y);
system.out.println("After swapping");
x=x+y;
y=x-y;
x=x-y;
System.out.println("value of x:"+x);
System.out.println("value of y:"+y);
}
}
Step 2
Name it as "swap.java"and save the file in any location i saved at "c:/app".
Step 3
Open command prompt(press window + R and write cmd and hit ok).

Step 4
Go to "c:/app" by using command prompt.

Step 5
Now write the following code for checking my java file is compiling or not.
javac swap.java

My java file is not compile successfully. because there are two error in this program.
So, let me write the program in a right way. and see the output.
//code
class demo
{
public static void main(String arg[])
{
System.out.println("Before swapping");
int x=10;
int y=20;
System.out.println("value of x:"+x);
System.out.println("value of y:"+y);
System.out.println("After swapping");
x=x+y;
y=x-y;
x=x-y;
System.out.println("value of x:"+x);
System.out.println("value of y:"+y);
}
}
Step 6
Now write the following code for checking my java file is compiling or not.
javac swap.java

My java program compiled successfully.
Step 7
Write the following code in command prompt. prees enter and see the output.
java demo
// demo is a class name which is written in my "swap.java" file.
Output

Happy coding.
Swap two numbers without using third variable in java
Lets try to find the error in this program. if we write the small 's' on the place of capital 'S'.
class demo
{
public static void main(string arg[])
{
System.out.println("Before swapping");
int x=10;
int y=20;
System.out.println("value of x:"+x);
System.out.println("value of y:"+y);
system.out.println("After swapping");
x=x+y;
y=x-y;
x=x-y;
System.out.println("value of x:"+x);
System.out.println("value of y:"+y);
}
}
Step 2
Name it as "swap.java"and save the file in any location i saved at "c:/app".
Step 3
Open command prompt(press window + R and write cmd and hit ok).

Step 4
Go to "c:/app" by using command prompt.

Step 5
Now write the following code for checking my java file is compiling or not.
javac swap.java

My java file is not compile successfully. because there are two error in this program.
So, let me write the program in a right way. and see the output.
//code
class demo
{
public static void main(String arg[])
{
System.out.println("Before swapping");
int x=10;
int y=20;
System.out.println("value of x:"+x);
System.out.println("value of y:"+y);
System.out.println("After swapping");
x=x+y;
y=x-y;
x=x-y;
System.out.println("value of x:"+x);
System.out.println("value of y:"+y);
}
}
Step 6
Now write the following code for checking my java file is compiling or not.
javac swap.java

My java program compiled successfully.
Step 7
Write the following code in command prompt. prees enter and see the output.
java demo
// demo is a class name which is written in my "swap.java" file.
Output

Happy coding.
Swap two numbers without using third variable in java
Answered by
0
package com.company;
/* Write a program to input two unequal numbers. Display the numbers after swapping their values in the variables without using a third variable.
Sample Input: a=23, b=56
Sample Output: a=56, b=23
*/
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter Number a:");
int a = sc.nextInt();
System.out.println("Enter Number b:");
int b = sc.nextInt();
if (a!=b){
a = a+b;
b = a-b;
a = a-b;
}
System.out.println("a = "+a+" b = "+b);
}
}
Similar questions