हिंदी

Fill an array (type int, size n). Perform Right Circular Shift.

Advertisements
Advertisements

प्रश्न

Fill an array (type int, size n).

Perform Right Circular Shift.

कोड लेखन
Advertisements

उत्तर

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
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 11: Programs Overall - Arrays [पृष्ठ २५२]

APPEARS IN

रुपा पंडित Computer Applications [English] Class 10 ICSE
अध्याय 11 Programs Overall
Arrays | Q 10. | पृष्ठ २५२
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×