Computer Science, asked by tanawosh1sadiqi, 6 months ago

write an algorithm which swaps the values of two numbers (java)!

Answers

Answered by manuprajapati519
0

Answer:

int a, b,c;

c=a;

a=b;

b=c

System.out.println(a);

System.out.println(b);

Hello this was only algorithm for swapping the numbers using third variable in Java, Hope my answer helped you, if you liked my answer so pls mark my answer as Brainliest and also pls follow me for more answers like this.

I joined few months back but don't have any follower yet except my brother.

THANK YOU

Answered by atrs7391
0

This actually was in my exam, here it is:

package com.company;

/* 11. b) 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 Answer11b {

   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