Computer Science, asked by prasannasagi2002, 9 months ago

Write a java program swapping of two numbers with out temporary variable( nonstatic method)

Answers

Answered by IamGenesis
0

Answer:

I am only writing the main code

These are two integers.....you need to at first input their value.....I am not writing that part.....

int a;

int b;

a = a+b;

b = a-b;

a = a-b;

the above three lines in the swap code.....

now they value of a and b is swapped........

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 with 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();

       int n = 0; n = a; a = b; b = n;        

       System.out.println("a = "+a+"    b = "+b);

   }

}

Similar questions