Pages

Saturday, 24 December 2016

Problem 48 - Swap

     Swap adalah program yang menukar tempat nilai dari A -> B dan B -> A

Code di bawah ini :

DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. import java.util.Scanner;
  2.  
  3. class SwapNumbers
  4. {
  5.    public static void main(String args[])
  6.    {
  7.       int x, y, temp;
  8.       System.out.println("Enter x and y");
  9.       Scanner in = new Scanner(System.in);
  10.  
  11.       x = in.nextInt();
  12.       y = in.nextInt();
  13.  
  14.       System.out.println("Before Swapping\nx = "+x+"\ny = "+y);
  15.  
  16.       temp = x;
  17.       x = y;
  18.       y = temp;
  19.  
  20.       System.out.println("After Swapping\nx = "+x+"\ny = "+y);
  21.    }
  22. }

Disini saya test input 5 sama 6


0 comments:

Post a Comment