Answers for "swap three variables in java without using temporary variable"

1

swap two variables without temporary java

import java.*; 
  
class noTemp { 
  
    public static void main(String a[]) 
    { 
        int x = 10; 
        int y = 5; 
        x = x + y; 
        y = x - y; 
        x = x - y; 
        System.out.println("After swaping:"
                           + " x = " + x + ", y = " + y); 
    } 
}
Posted by: Guest on January-22-2021
0

swapping with two variables in java

public class Exercise15 {
  public static void main(String[] args) {
     // int, double, float
   int a, b;
   a = 15;
   b = 27;
   System.out.println("Before swapping : a, b = "+a+", "+ + b);
   a = a + b;
   b = a - b;
   a = a - b;
   System.out.println("After swapping : a, b = "+a+", "+ + b);
 }
 
}
Posted by: Guest on January-23-2021
0

Swapping of two numbers in java with temporary variable

public class Main{public static void main(String[] args) {float a = 3.66f, b = 6.47f;System.out.println("a number " + a);System.out.println("b number " + b);float temp = a;a = b;b = temp;System.out.println("After swapping");System.out.println("a number " + a);System.out.println("b number " + b);}}
Posted by: Guest on June-12-2021
0

swap three variables in java without using temporary variable

// swap three variables in java without using temporary variable
public class SwapThreeNumbersWithoutTemp
{
   static int num1, num2, num3;
   public static void main(String[] args) 
   {
      num1 = 30; num2 = 60; num3 = 90; 
      System.out.println("Before swapping three numbers: num1 = " + num1 + ", num2 = " + num2 + ", num3 = " + num3);
      swapWithoutTemporary();
      System.out.println("After swapping three numbers: num1 = " + num1 + ", num2 = " + num2 + ", num3 = " + num3);
   }
   static void swapWithoutTemporary() 
   { 
      num1 = num1 + num2 + num3; 
      num2 = num1 - (num2 + num3); 
      num3 = num1 - (num2 + num3); 
      num1 = num1 - (num2 + num3);  
   }
}
Posted by: Guest on February-22-2021

Code answers related to "swap three variables in java without using temporary variable"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language