Computer Science, asked by sharib60, 1 year ago

write a program java to swap the value of two number​


Anonymous: ___k off

Answers

Answered by Stuxnet
2

Hey!! I am giving in this answer according to BLUE J compiler. If you Don't need this then comment me.

class prg

{

void main(int a,int b)

{

int c;

System.out.println(a);

System.out.println(b);

c = a;

a = b;

b= c;

System.out.println("new value"+a);

System.out.println("new value"+b);

}

}

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