Advertisements
Advertisements
Question
Fill an array (type int, size n).
Perform Left Circular Shift.
Code Writing
Advertisements
Solution
import java.util.*;
class clArraysSelection
{
public void main()
{
Scanner sc = new Scanner(System.in);
System.out.print(" Enter the array size : ");
int n = sc.nextInt();
int R[] = new int[n];
int j = 0;
for (j = 0; j < n; j++)
{
System.out.print("Enter value for cell number " + (j) + “ : ");
R[j] = sc.nextInt();
}
System.out.println("/t The array is: ");
for(j = 0; j<n;j++)
{
System.out.print(" " + R[j]);
}
//left circular shift
int tmp = R[O];
for(j= 1; j<n; j++)
{
R[j -1] = R[j];
}
R[n - 1] = tmp;
System.out.println("\n\n The array after a left circular shift : ");
for(j = 0; j<n; j++)
{
System.out.print("/t " + R[j]);
}
}
}
Output:
The array is :
99 22 77 44 55 11
The array after a Left Circular Shift : 22 77 44 55 11 99
shaalaa.com
Is there an error in this question or solution?
