write a program in java to display the number after swapping without using third variable
Question
write a program in java to display the number after swapping without using third variable
Solution
To swap two numbers without using a third variable in Java, you can follow these steps:
- Declare and initialize two variables, let's say
aandb, with the numbers you want to swap. - Print the original values of
aandbfor reference. - Perform the swap using the following steps:
- Assign the sum of
aandbtoa. - Assign the difference between the new value of
aand the original value ofbtob. - Assign the difference between the original value of
aand the new value ofbtoa.
- Assign the sum of
- Print the swapped values of
aandb.
Here's the code that implements the above steps:
public class SwapNumbers {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("Before swapping:");
System.out.println("a = " + a);
System.out.println("b = " + b);
// Swapping without using a third variable
a = a + b;
b = a - b;
a = a - b;
System.out.println("After swapping:");
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
When you run this program, it will display the numbers after swapping without using a third variable.
Similar Questions
How do you swap two numbers without using a third variable in Java?
Arun is working on a program to swap the values of two variables, a and b, without using a temporary variable. Write a program to help Arun accomplish this task.Input format :The first line of input is an integer value a.The second line of input is an integer value b.Output format :The output prints the values of a and b after swapping. Refer to the sample output for the formatting specifications.
Print the Answer which is formed by swapping Alternate digits of InputINPUT : INTEGEROUTPUT : INTEGERCONSTRAINTS : INPUT > 0Sample input12345 Sample output21435Explanationthe swapping starts from front end12 swapped will give 2134 swapped will give 435 is left there is nothing to swap with so it will be added to answer as it isNOTE : THE OUTPUT SHOULD BE A SINGLE INTEGERNote:Your code must be able to print the sample output from the provided sample input. However, your code is run against multiple hidden test cases. Therefore, your code must pass these hidden test cases to solve the problem statement.LimitsTime Limit: 5.0 sec(s) for each input fileMemory Limit: 256 MBSource Limit: 1024 KBScoringScore is assigned if any testcase passesAllowed LanguagesC
Thara is attending a math lecture, and her professor asks students to swap two numbers using a bitwise operator. The professor finds it difficult to check the logic of 50 students. So, he decided to write a Java program to get two numbers, first and second, from the user and swap the numbers. He is not good at programming. Help them by developing an application using your Java skills.Note:Use bitwise operator.Negative and zero are also accepted as inputs and numbers have to be swapped accordingly.In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.Ensure to follow the object-oriented specifications provided in the question description.Ensure to provide the names for classes, attributes, and methods as specified in the question description.Adhere to the code template, if provided.Please do not use System.exit(0) to terminate the program.Sample Input 1:Enter the numbers8743Sample Output 1:Before swappingfirst = 87, second = 43After swappingfirst = 43, second = 87
Write a C++ program that takes two integers as input and swaps their values without using a temporary variable. sample input and output Enter two integers: 10 20 Before swapping: num1 = 10, num2 = 20After swapping: num1 = 20, num2 = 10
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.