Scala program to Cyclically Permutes the Elements of an Array

Here, we are going to learn how to cyclically permutes the elements of an array in Scala programming language?
Submitted by Nidhi, on May 09, 2021 [Last updated : March 10, 2023]

Scala – Cyclically Permutes Array Elements

Here, we will create an array of integers and then we will permute the elements of the array cyclically.

Scala code to cyclically permutes the elements of an array

The source code to Cyclically Permute the Elements of an Array is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to Cyclically Permute 
// the Elements of an Array

object Sample {  
    def main(args: Array[String]) {  
        var IntArray = Array(31,15,42,14,23)
        var i:Int=0
        var t:Int=0
        
        println("Array elements before Cyclically Permutation: ");
        i=0;
        while(i<5)
        {
            printf("%d ",IntArray(i));
            i=i+1;
        }
        
        i=0;
        t = IntArray(0);
        while(i<5)
        {
            if(i==4)
                IntArray(i)=t;
            else
                IntArray(i)=IntArray(i+1);
            
            i=i+1;
        }
        
        println("\nArray elements after Cyclically Permutation: ");
        i=0;
        while(i<5)
        {
            printf("%d ",IntArray(i));
            i=i+1;
        }
        println()
    }
}

Output

Array elements before Cyclically Permutation: 
31 15 42 14 23 
Array elements after Cyclically Permutation: 
15 42 14 23 31 

Explanation

In the above program, we used an object-oriented approach to create the program. We created an object Sample, and we defined main() function. The main() function is the entry point for the program.

In the main() function, we created an array IntArray that contains 5 integer items. Then we cyclically permute the elements of the array and then print them on the console screen.

Scala Array Programs »



Related Programs



All Scala Programs

Comments and Discussions!

Load comments ↻





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