Computer Science, asked by kumarcaptain430, 1 year ago

Write a program to interchange 2 variables without using the third one.

Answers

Answered by sikhi
0

Answer:

package com.sample.sample2;

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {

       Scanner scan=new Scanner(System.in);

       System.out.println("Enter variable 1");

       int v1=scan.nextInt();

       System.out.println("Enter variable 2");

       int v2=scan.nextInt();

       v1=v1+v2;

       v2=v1-v2;

       v1=v1-v2;

       System.out.println("After swapping :\n variable1="+v1+"\nvariable2="+v2);

   }

}

Explanation:

Answered by anindyaadhikari13
1

\star\:\:\:\sf\large\underline\blue{Question:-}

  • Write a program to Interchange 2 variables without using third variable.

\star\:\:\:\sf\large\underline\blue{Code:-}

  • The given code is written in Java language.

import java.util.*;

class Swap

{

public static void main(String s[])

{

Scanner sc = new Scanner(System.in);

System.out.print("Enter the value of a: ");

int a=sc.nextInt();

System.out.print("Enter the value of b: ");

int b=sc.nextInt();

System.out.println("Before swapping, ");

System.out.println("a: "+a);

System.out.println("b: "+b);

a+=b;

b=a-b;

a-=b;

System.out.println("After swapping, ");

System.out.println("a: "+a);

System.out.println("b: "+b);

}

}

Similar questions