Skip to content

Commit

Permalink
Updated and Formatted Swap.java (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
aliul21 authored Oct 24, 2022
1 parent e5aea87 commit 1de70f1
Showing 1 changed file with 43 additions and 46 deletions.
89 changes: 43 additions & 46 deletions Java/Multiple Swapping Methods/swap.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,53 @@
import java.util.*;

class Swap {
/**
* @param args
*/
public static void main(String args[]) {

Scanner aliul12 = new Scanner (System.in);

System.out.println("select the method of swapping two numbers");
System.out.println("press 1 for + & - method");
System.out.println("press 2 for * & / method");
System.out.println("press 3 for XOR method");
System.out.println("press 4 for single statement method");
System.out.println("Enter your choice: ");
public static void main(String args[]) {
Scanner sc = new Scanner (System.in);

System.out.println("Select the method of swapping two numbers.");
System.out.println("\t 1. Press 1 for + & - method.");
System.out.println("\t 2. Press 2 for * & / method.");
System.out.println("\t 3. Press 3 for XOR method.");
System.out.println("\t 4. Press 4 for single statement method.");
System.out.println("Enter your choice: ");
int choice = sc.nextInt();

int choice = aliul12.nextInt();
int a,b;
System.out.println("Enter 1st no. A:");
a = aliul12.nextInt();
System.out.println("Enter 2nd no. B:");
b = aliul12.nextInt();

switch(choice) {

// + & - method
case 1: a=a+b;
b=a-b;
a=a-b;
System.out.println("result: "+a+" "+b);
break;
// * & / method
case 2: a=a*b;
b=a/b;
a=a/b;
System.out.println("result: "+a+" "+b);
break;
// XOR method
case 3: a=a^b;
b=a^b;
a=a^b;
System.out.println("result: "+a+" "+b);
break;

// single statement method
case 4: b=a+b-(a=b);
System.out.println("result: "+a+" "+b);
break;
default:System.out.println("Invalid choice");
break;

}
a = sc.nextInt();
System.out.println("Enter 2nd no. B:");
b = sc.nextInt();

switch(choice) {
// + & - method
case 1: a=a+b;
b=a-b;
a=a-b;
System.out.println("result: "+a+" "+b);
break;
// * & / method
case 2: a=a*b;
b=a/b;
a=a/b;
System.out.println("result: "+a+" "+b);
break;
// XOR method
case 3: a=a^b;
b=a^b;
a=a^b;
System.out.println("result: "+a+" "+b);
break;

// single statement method
case 4: b=a+b-(a=b);
System.out.println("result: "+a+" "+b);
break;

default:System.out.println("Invalid choice");
break;

}

}

Expand Down

0 comments on commit 1de70f1

Please sign in to comment.