Write a Kotlin Program to Display Characters from A to Z using Loop

In Kotlin, we can display characters from A to Z using a loop.

The easiest way to do this is by using the for loop.

Here’s the Kotlin code to display characters from A to Z:

fun main() {
    for (c in 'A'..'Z') {
        print("$c ")
    }
}

Let’s break down the code:

  • We first define a main() function, which is the entry point of our program.
  • Inside the function, we use a for loop to iterate over characters from ‘A’ to ‘Z’.
  • The loop variable c takes the value of each character in the range ‘A’..’Z’.
  • Inside the loop, we use the print() function to display the value of c.
  • We also add a space after each character so that they are separated.

When we run the above code, we will get the output as follows:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

In conclusion, by using a simple for loop, we can easily display characters from A to Z in Kotlin.