Computer Science, asked by banunaseema627, 3 months ago

write a java program to swap two numbers with output using third variable by using comment lines


plz don't post anything if u don't know... if you post it ur answer will be reported.. ​

Answers

Answered by atrs7391
2

Your program using Third Variable:

ackage com.company;

import java.util.Scanner;

public class main {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       System.out.println("Enter 1st number: ");

       int n = sc.nextInt();

       System.out.println("Enter 2nd number: ");

       int n2 = sc.nextInt();

       int n3;

       n3 = n;

       n = n2;

       n2 = n3;

       System.out.println("After swapping 1st number is: "+n);

       System.out.println("After swapping 2nd number is: "+n2);

   }

}

Your program without using Third Variable:

package com.company;  

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {

       Scanner sc = new Scanner (System.in);

       System.out.println("Enter Number 1:");

       int a = sc.nextInt();

       System.out.println("Enter Number 2:");

       int b = sc.nextInt();

       if (a!=b){

           a = a+b;

           b = a-b;

           a = a-b;

       }

       System.out.println("Numbers after swapping");

       System.out.println("1st Number = "+a+"    2nd Number = "+b);

   }

}

I was not able to understand what you means by using comment lines so I gave two programs.

Similar questions