Saturday 10 March 2012

Swap Numbers using third variable

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.kota.java;

public class SwapNumbers {

  public static void main(String[] args) {
         
         int num1 = 10;
         int num2 = 20;
        
         System.out.println("Before Swapping");
         System.out.println("Value of num1 is :" + num1);
         System.out.println("Value of num2 is :" +num2);
        
         //swap the value
         swap(num1, num2);
 }

 private static void swap(int num1, int num2) {
        
         int temp = num1;
         num1 = num2;
         num2 = temp;
        
         System.out.println("After Swapping");
         System.out.println("Value of num1 is :" + num1);
         System.out.println("Value of num2 is :" +num2);
        
 }
}
/* Output :
Before Swapping
Value of num1 is :10
Value of num2 is :20
After Swapping
Value of num1 is :20
Value of num2 is :10*/

2 comments:

  1. Hi
    I faced one interview question how to write program for string words reverse.
    Like input : "Hello java world"
    Output : "world java Hello"

    thanks
    kesav

    ReplyDelete
  2. Can you please write a program for me.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...