Menu

Drop Down MenusCSS Drop Down Menu Pure CSS Dropdown Menu

Java

Program: How to swap two numbers without using temporary variable?

Description:
Write a program to swap or exchange two numbers. You should
not use any temporary or third variable to swap.

public class MySwapingTwoNumbers {

    public static void main(String a[]){
        int x = 10;
        int y = 20;
        System.out.println("Before swap:");
        System.out.println("x value: "+x);
        System.out.println("y value: "+y);
        x = x+y;
        y=x-y;
        x=x-y;
        System.out.println("After swap:");
        System.out.println("x value: "+x);
        System.out.println("y value: "+y);
    }
}

Bubble sort algorithm is known as the simplest sorting algorithm.
In bubble sort algorithm, array is traversed from first element to last element. Here, current element is compared with the next element. If current element is greater than the next element, it is swapped.


public class BubbleSortExample {  
    static void bubbleSort(int[] arr) {  
        int n = arr.length;  
        int temp = 0;  
         for(int i=0; i < n; i++){  
                 for(int j=1; j < (n-i); j++){  
                          if(arr[j-1] > arr[j]){  
                                 //swap elements  
                                 temp = arr[j-1];  
                                 arr[j-1] = arr[j];  
                                 arr[j] = temp;  
                         }  
                          
                 }  
         }  
  
    }  
    public static void main(String[] args) {  
                int arr[] ={3,60,35,2,45,320,5};  
                 
                System.out.println("Array Before Bubble Sort");  
                for(int i=0; i < arr.length; i++){  
                        System.out.print(arr[i] + " ");  
                }  
                System.out.println();  
                  
                bubbleSort(arr);//sorting array elements using bubble sort  
                 
                System.out.println("Array After Bubble Sort");  
                for(int i=0; i < arr.length; i++){  
                        System.out.print(arr[i] + " ");  
                }  
   
        }  



FizzBuzz problem : Write a Java program that prints the numbers from 1 to 50. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"

This is also one of the classical programming questions, which is asked on any Java programming or technical interviews. This questions is very basic but can be very trick for programmers, who can't code, that's why it is used to differentiate programmers who can do coding and who can't.

Here is a sample Java program to solve FizzBuzz problem :

public class FizzBuzzTest{
 
    public static void main(String args[]){
    
        for(int i = 1; i <= 50; i++) {
            if(i % (3*5) == 0) System.out.println("FizzBuzz");
            else if(i % 5 == 0) System.out.println("Buzz");
            else if(i % 3 == 0) System.out.println("Fizz");
            else System.out.println(i);
        } 
    } 

}




Description:
Write a program to swap or exchange two numbers. You should
not use any temporary or third variable to swap.
- See more at: http://www.java2novice.com/java-interview-programs/swap-two-numbers/#sthash.RtG9qrP0.dpuf
Program: How to swap two numbers without using temporary variable? - See more at: http://www.java2novice.com/java-interview-programs/swap-two-numbers/#sthash.RtG9qrP0.dpuf
Program: How to swap two numbers without using temporary variable? - See more at: http://www.java2novice.com/java-interview-programs/swap-two-numbers/#sthash.RtG9qrP0.dpuf
Program: How to swap two numbers without using temporary variable? - See more at: http://www.java2novice.com/java-interview-programs/swap-two-numbers/#sthash.RtG9qrP0.dpuf

No comments:

Post a Comment

Allah Is Almighty