In this article, we will learn to write a Kotlin program to reverse an Array.

Source Code:

Description:

Source Code:

fun main(args: Array<String>) {

val numbers = intArrayOf(11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

println("Array before reverse:")

for (i in numbers.indices) {

print(numbers[i].toString() + ", ")

}

//Logic to reverse an array

for (i in 0 until numbers.size / 2) {

val temp = numbers[i]

numbers[i] = numbers[numbers.size - 1 - i]

numbers[numbers.size - 1 - i] = temp

}

println("\nArray after reverse:")

for (i in numbers.indices) {

print(numbers[i].toString() + ", ")

}

}

Output:val numbers = intArrayOf(11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

println("Array before reverse:")

for (i in numbers.indices) {

print(numbers[i].toString() + ", ")

}

//Logic to reverse an array

for (i in 0 until numbers.size / 2) {

val temp = numbers[i]

numbers[i] = numbers[numbers.size - 1 - i]

numbers[numbers.size - 1 - i] = temp

}

println("\nArray after reverse:")

for (i in numbers.indices) {

print(numbers[i].toString() + ", ")

}

}

Array before reverse:

11, 12, 13, 14, 15, 16, 17, 18, 19, 20,

Array after reverse:

20, 19, 18, 17, 16, 15, 14, 13, 12, 11,

11, 12, 13, 14, 15, 16, 17, 18, 19, 20,

Array after reverse:

20, 19, 18, 17, 16, 15, 14, 13, 12, 11,

Description:

In the above source code, we have declared an array. Which holds 10 numbers. In a next step, we have printed the same array using print() function before reversing it. Next, we have used for loop to reverse the order of array elements. To understand the logic of reversing an order of array element, read and understand below program with an output.

### Simple java program to reverse an array: Step by Step

Source Code:
fun main(args: Array<String>) {

val numbers = intArrayOf(11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

println("Array before reverse:")

for (i in numbers.indices) {

print(numbers[i].toString() + ", ")

}

//Logic to reverse an array

println("\nReverse array step by step:")

for (i in 0 until numbers.size / 2) {

val temp = numbers[i]

numbers[i] = numbers[numbers.size - 1 - i]

numbers[numbers.size - 1 - i] = temp

for (i in numbers.indices) {

print(numbers[i].toString() + ", ")

}

println()

}

println("\nArray after reverse:")

for (i in numbers.indices) {

print(numbers[i].toString() + ", ")

}

}

Output:val numbers = intArrayOf(11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

println("Array before reverse:")

for (i in numbers.indices) {

print(numbers[i].toString() + ", ")

}

//Logic to reverse an array

println("\nReverse array step by step:")

for (i in 0 until numbers.size / 2) {

val temp = numbers[i]

numbers[i] = numbers[numbers.size - 1 - i]

numbers[numbers.size - 1 - i] = temp

for (i in numbers.indices) {

print(numbers[i].toString() + ", ")

}

println()

}

println("\nArray after reverse:")

for (i in numbers.indices) {

print(numbers[i].toString() + ", ")

}

}

Array before reverse:

11, 12, 13, 14, 15, 16, 17, 18, 19, 20,

Reverse array step by step:

20, 12, 13, 14, 15, 16, 17, 18, 19, 11,

20, 19, 13, 14, 15, 16, 17, 18, 12, 11,

20, 19, 18, 14, 15, 16, 17, 13, 12, 11,

20, 19, 18, 17, 15, 16, 14, 13, 12, 11,

20, 19, 18, 17, 16, 15, 14, 13, 12, 11,

Array after reverse:

20, 19, 18, 17, 16, 15, 14, 13, 12, 11,

11, 12, 13, 14, 15, 16, 17, 18, 19, 20,

Reverse array step by step:

20, 12, 13, 14, 15, 16, 17, 18, 19, 11,

20, 19, 13, 14, 15, 16, 17, 18, 12, 11,

20, 19, 18, 14, 15, 16, 17, 13, 12, 11,

20, 19, 18, 17, 15, 16, 14, 13, 12, 11,

20, 19, 18, 17, 16, 15, 14, 13, 12, 11,

Array after reverse:

20, 19, 18, 17, 16, 15, 14, 13, 12, 11,

Description: In the above program, we have tried to demonstarte the logic of reversing elements of array step by step, and with output to compare it.

## 0 Comments