Computer Science, asked by BRAINLIEST77, 1 year ago

Can anyone write a java program for swapping the values of two variables.

Answers

Answered by Aryanmonu
2
class SwapNumbers { public static void main(String args[]) { int x, y, temp; System.out.println("Enter x and y"); Scanner in = new Scanner(System.in);   x = in.nextInt(); y = in.nextInt();   System.out.println("Before Swapping\nx = "+x+"\ny = "+y);   temp = x; x = y; y = temp;   System.out.println("After Swapping\nx = "+x+"\ny = "+y); } }

BRAINLIEST77: The one without scanner method. Just a simple one please?
Answered by atrs7391
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