Kotlin Program to Display Alphabets A to Z Using Loop



In this article, we will understand how to print alphabets from A to Z or a to z in Kotlin. This is accomplished using a simple for-loop.

Below is a demonstration of the same

Suppose our input is

A to Z

The desired output would be

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

Algorithm

  • Step 1 ? Start

  • Step 2 ? Declare a character variable: input

  • Step 3 ? Run a while loop with condition input greater that or equal to Z and print all the values satisfying the condition.

  • Step 4 ? Display the result

  • Step 5 ? Stop

Example 1

Here, we have displayed alphabets using a while loop in Kotlin. First declare and set a char variable:

var input: Char
input = 'A'

Now, use the while loop to print all the alphabets from A to Z ?

while (input < = 'Z') { print("$input ") ++input }

Let us now see the complete example

fun main() { var input: Char input = 'A' println("Displaying Alphabets from A to Z ") while (input <= 'Z') { print("$input ") ++input } }

Output

Displaying Alphabets from A to Z
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

Example 2

Here, we have displayed alphabets A - Z in Kotlin ?

fun main() { var input: Char input = 'A' printAlphabets(input) } fun printAlphabets(inputCharacter: Char) { var input = inputCharacter println("Displaying Alphabets from A to Z ") while (input <= 'Z') { print("$input ") ++input } }

Output

Displaying Alphabets from A to Z
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
Updated on: 2022-10-13T12:47:18+05:30

984 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements