Circular shift program

JAVA videos

Circular shift
A class Circular has been signed to print a set of elements in a circular fashion. For example, if input is 1, 2,3,4 output should be:
1,2,3,4
4,1,2,3
3,4,1,2
2,3,4,1
Class name: CircularShift
Data members/ instance variables:

list[ ]: store the elements
size: to store the number of elements

Member functions:
void input(): to input the size and the elements in the array
void Circular_Shift():to shift the elements of the array one place to the right. First element comes to the second position, second element to the third and ultimately the last element comes to the first position and so on.
void reverse(): reverse the array without using any temporary variable to swap its elements.
void display(): to display the array in a circular fashion.
Specify the class CircularShift giving details of the constructor and member
functions void input(), void Circular_Shift(), void reverse(). void display() define the main() function to create an object and call the functions accordingly.

import java.util.Scanner;
class CircularShift
{
    static int list[]; 
    static int size;
    void input()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of elements");
        size = sc.nextInt();
        list = new int[size];
        System.out.println("Enter the elements");
        for(int i = 0;i<size;i++)
        {
            list[i] = sc.nextInt(); 
        }
    }

    void Circular_Shift()
    {
        int temp=list[size-1];
        for(int i=size-1;i>0;i--)
        {
            list[i]=list[i-1];
        }
        list[0]=temp; 
    }

    void reverse()
    {
        for (int i = 0; i < size/2; i++) 
        {
            int temp = list[i];
            list[i] = list[size - 1 - i];
            list[size - 1 - i] = temp;
        }
    }

    void display()
    {
        System.out.println("The array is as follows :");
        for(int i = 0;i<=size;i++)
        {
            if(i!=0 && i<size)
            {
                Circular_Shift();  
                for(int j = 0;j<size;j++)
                {
                    System.out.print(list[j]+",");  
                }
                System.out.println("");
            }
            if(i==0)
            {
                for(int j = 0;j<size;j++)
                {
                    System.out.print(list[j]+",");
                }
                System.out.println();
            }
            if(i==(size-1))
            {
                Circular_Shift();
            }
        }
    }

    public static void main()
    {
        CircularShift obj = new CircularShift();
        obj.input();
        obj.display();
        obj.reverse();
        System.out.println("Reversed array :");
        for(int i = 0;i<size;i++)
        {
            System.out.print(list[i]+","); 
        }
    }
}
Circular shift

Circular prime number program

LEAVE A REPLY

Please enter your comment!
Please enter your name here