Todays Program:

Left Shifting the given numbers:

package demo;
//left swift
import java.util.Arrays;

public class singleReverse {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[]ar= {10,20,30,40,50};
    int temp=ar[0];
    for(int i=0;i<ar.length-1;i++) {
        ar[i]=ar[i+1];

    }
    ar[ar.length-1]=temp;
    System.out.println(Arrays.toString(ar));

}

}

output:

[20, 30, 40, 50, 10]

right shifting the given numbers:

package demo;
//right swift
import java.util.Arrays;

public class rightshift {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] ar= {10,20,30,40,50};
    int temp=ar[ar.length-1];
    for(int i=ar.length-1;i>0;i--) {
        ar[i]=ar[i-1];

    }
    ar[0]=temp;
    System.out.println(Arrays.toString(ar));

}

}

output:

[50, 10, 20, 30, 40]

Righting twice the given numbers:

package demo;

import java.util.Arrays;
//right twice shift
public class rightswift {

public static void main(String args[]) {

  int[] ar= {10,20,30,40,50};
  int temp1=ar[ar.length-2];
  int temp2=ar[ar.length-1];
  for(int i=ar.length-1;i>1;i--) {
      ar[i]=ar[i-2];

  }
  ar[0]=temp1;
  ar[1]=temp2;
  System.out.println(Arrays.toString(ar));

}

}

output:

[40, 50, 10, 20, 30]

left shifting the given two number:

package demo;

import java.util.Arrays;

public class lefttwiceshift {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] ar= {10,20,30,40,50};
    int temp1=ar[0];
    int temp2=ar[1];
    for(int i=0;i<ar.length-2;i++) {
        ar[i]=ar[i+2];

    }
    ar[ar.length-2]=temp1;
    ar[ar.length-1]=temp2;
    System.out.println(Arrays.toString(ar));
}

}

output:

[30, 40, 50, 10, 20]


Leave a comment

Design a site like this with WordPress.com
Get started