Computer Science, asked by iamkarangrovee2954, 11 months ago

java program to input 2 numbers and swap their values

Answers

Answered by pallavi89
1

Answer:

import java.util.*;

curly bracket

public static void main()

curly bracket

Scanner sc = new Scanner(System.in);

System.out.println("enter two no");

int n1 = sc.nextInt();

int n2= sc.nextInt();

int temp;

t= n1;

n1= n2

n2= t

System. out.println(n1,n2);

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