Ruby program to cyclically permutes the elements of the array

Ruby Example: Write a program to cyclically permutes the elements of the array.
Submitted by Nidhi, on January 22, 2022

Problem Solution:

In this program, we will create an array of integers and then we will cyclically permute the elements of the array and print the result.

Program/Source Code:

The source code to cyclically permute the elements of the array is given below. The given program is compiled and executed successfully.

# Ruby program to Cyclically Permute the 
# elements of array

arr = [12,69,49,87,68];

print "Array elements before Cyclically Permutation: \n";
i=0;
while(i<5)
    print arr[i]," ";
    i=i+1;
end

i=0;
t = arr[0];
while(i<5)
    if(i==4)
        arr[i]=t;
    else
        arr[i]=arr[i + 1];
    end
    i = i + 1;
end
        
print "\nArray elements after Cyclically Permutation: \n";
i=0;
while(i<5)
    print arr[i]," ";
    i=i+1;
end

Output:

Array elements before Cyclically Permutation: 
12 69 49 87 68 
Array elements after Cyclically Permutation: 
69 49 87 68 12 

Explanation:

In the above program, we created an array of integer elements. Then we cyclically permutated the elements of the array. After that, we printed the result.

Ruby Arrays Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.