Advertisements
Advertisements
Question
Fill an array (type int, size n).
Perform Right Circular Shift.
Code Writing
Advertisements
Solution
import java.util.*;
class clArraysRightShift
{
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]);
}
int tmp = R[n - 1];
for(j = n - 1; j > 0; j--)
{
R[j] = R[j - 1];
}
R[0] = tmp;
System.out.println("\n\n The array after a right circular shift : ");
for(j = 0; j < n; j++)
{
System.out.print("\t" + R[j]);
}
}
}
Output:
Enter the array size : 5
Enter value for cell number 0 : 10
Enter value for cell number 1 : 20
Enter value for cell number 2 : 30
Enter value for cell number 3 : 40
Enter value for cell number 4 : 50
The array is :
10 20 30 40 50
The array after a right circular shift :
50 10 20 30 40
shaalaa.com
Is there an error in this question or solution?
