Computer Science, asked by Abhisekkumar, 1 year ago

Example of swaping of two numbers without using third variable in java?

Answers

Answered by vershamishra12oycd27
1
hey..I'll tell u logic
a=a+b
b=a-b
a=a-b
nw write acc to the Java's syntax

vershamishra12oycd27: mark it as brainliest if it helps u
Abhisekkumar: how I will help
vershamishra12oycd27: no i don't need any help if u found this ans useful then there s option of mark as a brainliest
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