Monday, April 26, 2010

Swap number using java program

1. /*
2. Swap Numbers Java Example
3. This Swap Numbers Java Example shows how to
4. swap value of two numbers using java.
5. */
6.
7. public class SwapElementsExample {
8.
9. public static void main(String[] args) {
10.
11. int num1 = 10;
12. int num2 = 20;
13.
14. System.out.println("Before Swapping");
15. System.out.println("Value of num1 is :" + num1);
16. System.out.println("Value of num2 is :" +num2);
17.
18. //swap the value
19. swap(num1, num2);
20. }
21.
22. private static void swap(int num1, int num2) {
23.
24. int temp = num1;
25. num1 = num2;
26. num2 = temp;
27.
28. System.out.println("After Swapping");
29. System.out.println("Value of num1 is :" + num1);
30. System.out.println("Value of num2 is :" +num2);
31.
32. }
33. }
34.
35. /*
36. Output of Swap Numbers example would be
37. Before Swapping
38. Value of num1 is :10
39. Value of num2 is :20
40. After Swapping
41. Value of num1 is :20
42. Value of num2 is :10
43. */

this program will swap number numbers.

No comments:

Post a Comment