Program to Print Alphabets From A to Z Using Loop

Last Updated : 17 Aug, 2026

The task is to print all English alphabets from A to Z or a to z using loops in C.

  • The alphabets can be printed using ASCII values or character variables.
  • This article covers three approaches: for loop, while loop, and do-while loop.

Approaches to Print Alphabets From A to Z Using Loop

1. Print Alphabets Using ASCII Values

In ASCII, uppercase alphabets range from 65 (A) to 90 (Z), while lowercase alphabets range from 97 (a) to 122 (z). We can iterate through these values and typecast each integer to char to print the corresponding alphabet.

C++
#include <iostream>

using namespace std;

int main()
{
    int i;
  
    cout << "Alphabets from (A-Z) are:\n";
  
    // ASCII value of A=65 and Z=90
    for (i = 65; i <= 90; i++) {
        // Integer i with %c will be converted to character
        // before printing.%c will takes its equivalent
        // character value
        cout << (char)i << " ";
    }

    cout << "\nAlphabets from (a-z) are:\n";

    // ASCII value of a=97 and z=122
    for (i = 97; i <= 122; i++) {
        // Integer i with %c will be converted to character
        // before printing.%c will takes its equivalent
        // character value
        cout << (char)i << " ";
    }
    return 0;
}
C
#include <stdio.h>

int main()
{
    int i;
    printf("Alphabets from (A-Z) are:\n");

    // ASCII value of A=65 and Z=90
    for (i = 65; i <= 90; i++) {
        // Integer i with %c will be converted to character
        // before printing.%c will takes its equivalent
        // character value
        printf("%c ", i);
    }

    printf("\nAlphabets from (a-z) are:\n");

    // ASCII value of a=97 and z=122
    for (i = 97; i <= 122; i++) {
        // Integer i with %c will be converted to character
        // before printing.%c will takes its equivalent
        // character value
        printf("%c ", i);
    }

    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        int i;
        System.out.println("Alphabets from (A-Z) are:");
        // ASCII value of A=65 and Z=90
        for (i = 65; i <= 90; i++) {
            // Integer i with %c will be converted to character
            // before printing.%c will takes its equivalent
            // character value
            System.out.print((char)i + " ");
        }
        System.out.println("\nAlphabets from (a-z) are:");
        // ASCII value of a=97 and z=122
        for (i = 97; i <= 122; i++) {
            // Integer i with %c will be converted to character
            // before printing.%c will takes its equivalent
            // character value
            System.out.print((char)i + " ");
        }
    }
}
Python
def main():
    print("Alphabets from (A-Z) are:")
    # ASCII value of A=65 and Z=90
    for i in range(65, 91):
        # Integer i with chr() will be converted to character
        # before printing. chr() will take its equivalent
        # character value
        print(chr(i), end=" ")

    print("\nAlphabets from (a-z) are:")
    # ASCII value of a=97 and z=122
    for i in range(97, 123):
        # Integer i with chr() will be converted to character
        # before printing. chr() will take its equivalent
        # character value
        print(chr(i), end=" ")


if __name__ == "__main__":
    main()
    
# This code is contributed by Dwaipayan Bandyopadhyay
C#
using System;

public class GFG
{
    public static void Main()
    {
        int i;

        Console.WriteLine("Alphabets from (A-Z) are:");
        
        // ASCII value of A=65 and Z=90
        for (i = 65; i <= 90; i++)
        {
            // Integer i with (char) will be converted to character
            // before printing. (char) will take its equivalent
            // character value
            Console.Write((char)i + " ");
        }

        Console.WriteLine("\nAlphabets from (a-z) are:");

        // ASCII value of a=97 and z=122
        for (i = 97; i <= 122; i++)
        {
            // Integer i with (char) will be converted to character
            // before printing. (char) will take its equivalent
            // character value
            Console.Write((char)i + " ");
        }
    }
}
JavaScript
console.log("Alphabets from (A-Z) are:");

// ASCII value of A=65 and Z=90
for (let i = 65; i <= 90; i++) {
    // Convert the ASCII value to a character and print it
    console.log(String.fromCharCode(i) + " ");
}

console.log("\nAlphabets from (a-z) are:");

// ASCII value of a=97 and z=122
for (let i = 97; i <= 122; i++) {
    // Convert the ASCII value to a character and print it
    console.log(String.fromCharCode(i) + " ");
}

Output
Alphabets from (A-Z) are:
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 
Alphabets from (a-z) are:
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 

Explanation: The loop starts from the ASCII value of the first alphabet and increments it by 1 until it reaches the last alphabet. The (char) typecast converts each ASCII value into its corresponding character.

2. Print Alphabets Using a for Loop

Instead of using ASCII values directly, we can initialize a char variable with 'A' and increment it until 'Z'. The same approach can be used for lowercase alphabets.

C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
    // Declare the variables
    char i;

    // Display the alphabets
    cout << "The Alphabets from A to Z are: \n";

    // Traverse each character
    // with the help of for loop
    for (i = 'A'; i <= 'Z'; i++) {
        // Print the alphabet
        cout << i << " ";
    }
    // Display the alphabets
    cout << "\nThe Alphabets from a to z are: \n";
    for (i = 'a'; i <= 'z'; i++) {
        // Print the alphabet
        cout << i << " ";
    }

    return 0;
}
C
#include <stdio.h>

int main()
{
    // Declare the variables
    char i;

    // Display the alphabets
    printf("The Alphabets from A to Z are: \n");

    // Traverse each character
    // with the help of for loop
    for (i = 'A'; i <= 'Z'; i++) {

        // Print the alphabet
        printf("%c ", i);
    }

    printf("\nThe Alphabets from a to z are: \n");

    // Traverse each character
    // with the help of for loop
    for (i = 'a'; i <= 'z'; i++) {

        // Print the alphabet
        printf("%c ", i);
    }

    return 0;
}
Java
class GFG {

    public static void main(String[] args)
    {
        // Declare the variables
        char i;

        // Display the alphabets
        System.out.printf("The Alphabets from A to Z are: \n");

        // Traverse each character
        // with the help of for loop
        for (i = 'A'; i <= 'Z'; i++) {
            // Print the alphabet
            System.out.printf("%c ", i);
        }
        // Display the alphabets
        System.out.printf("\nThe Alphabets from a to z are: \n");

        // Traverse each character
        // with the help of for loop
        for (i = 'a'; i <= 'z'; i++) {
            // Print the alphabet
            System.out.printf("%c ", i);
        }
    }
}
Python
if __name__ == '__main__':
    
    # Declare the variables
    i = chr;

    # Display the alphabets
    print("The Alphabets from A to Z are: ");

    # Traverse each character
    # with the help of for loop
    for i in range(ord('A'), ord('Z') + 1):

        # Print the alphabet
        print(chr(i), end=" ");
    # Display the alphabets
    print("\nThe Alphabets from a to z are: ");

    # Traverse each character
    # with the help of for loop
    for i in range(ord('a'), ord('z') + 1):

        # Print the alphabet
        print(chr(i), end=" ");
        
C#
using System;
class GFG
{

    public static void Main(String[] args) 
    {
        // Declare the variables
        char i;

        // Display the alphabets
        Console.Write("The Alphabets from A to Z are: \n");

        // Traverse each character
        // with the help of for loop
        for (i = 'A'; i <= 'Z'; i++)
        {

            // Print the alphabet
            Console.Write("{0} ", i);
        }
      // Display the alphabets
        Console.Write("\nThe Alphabets from a to z are: \n");

        // Traverse each character
        // with the help of for loop
        for (i = 'a'; i <= 'z'; i++)
        {

            // Print the alphabet
            Console.Write("{0} ", i);
        }

    }
}
JavaScript
<script>

// Javascript program to find the print
// Alphabets from A to Z

// Declare the variables
let i;

// Display the alphabets
document.write("The Alphabets from A" +
               " to Z are: " + "</br>");

// Traverse each character
// with the help of for loop
for(i = 'A'.charCodeAt(); 
    i <= 'Z'.charCodeAt(); i++)
{
    
    // Print the alphabet
    document.write(
        String.fromCharCode(i)  + " ");
}

// Display the alphabets
document.write("The Alphabets from a" +
               " to z are: " + "</br>");

// Traverse each character
// with the help of for loop
for(i = 'a'.charCodeAt(); 
    i <= 'z'.charCodeAt(); i++)
{
    
    // Print the alphabet
    document.write(
        String.fromCharCode(i)  + " ");
}

</script>

Output
The Alphabets from A to Z are: 
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 
The Alphabets from a to z are: 
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 

Explanation: The for loop starts with 'A' and increments the character until 'Z'. Since consecutive English alphabet characters have consecutive character codes, incrementing the char variable prints the alphabets in order.

3. Print Alphabets Using a while Loop

We can also use a while loop to print alphabets. The character variable is initialized with the starting alphabet and incremented after each iteration.

C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
    // Declare the variables
    char i;

    // Display the alphabets
    cout << "The Alphabets from A to Z are: \n";

    // Traverse each character
    // with the help of while loop
    i = 'A';

    while (i <= 'Z') {
        cout << i << ' ';
        i++;
    }
    // Display the alphabets
    i = 'a';

    cout << "\nThe Alphabets from a to z are: \n";

    while (i <= 'z') {
        cout << i << ' ';
        i++;
    }

    return 0;
}
C
#include <stdio.h>

int main()
{
    // Declaring the variable
    char i;

    // Display the alphabets
    printf("The Alphabets from A to Z are: \n");

    // Traversing each character
    // with the help of while loop

    i = 'A';

    while (i <= 'Z') {
        printf("%c ", i);
        i++;
    }

    // for lower case alphabets
    printf("\nThe Alphabets from a to z are: \n");

    i = 'a';

    while (i <= 'z') {
        printf("%c ", i);
        i++;
    }

    return 0;
}
Java
import java.io.*;
public class GFG {
    public static void main(String[] args) {
        // Declare the variables
        char i;
        System.out.println("The Alphabets from A to Z are: ");
        // Traverse each character using a 
       // while loop
        i = 'A';
        while (i <= 'Z') {
            System.out.print(i + " ");
            i++;
        }
        // Display the lowercase alphabets
        i = 'a';
        System.out.println("\nThe Alphabets from a to z are: ");
        while (i <= 'z') {
            System.out.print(i + " ");
            i++;
        }
    }
}
Python
i = 'A'

# Display the alphabets
print "The Alphabets from A to Z are:"

# Traverse each character
# with the help of a while loop
while ord(i) <= ord('Z'):
    print i,
    i = chr(ord(i) + 1)

# Display the alphabets
i = 'a'

print "\nThe Alphabets from a to z are:"

while ord(i) <= ord('z'):
    print i,
    i = chr(ord(i) + 1)
C#
using System;

class Program {
    static void Main()
    {
        // Declare the variable
        char i;

        // Display the alphabets from A to Z
        Console.WriteLine("The Alphabets from A to Z are:");

        // Traverse each character using a while loop
        i = 'A';

        while (i <= 'Z') {
            Console.Write(i + " ");
            i++;
        }

        // Display the alphabets from a to z
        i = 'a';

        Console.WriteLine(
            "\nThe Alphabets from a to z are:");

        while (i <= 'z') {
            Console.Write(i + " ");
            i++;
        }
    }
}
JavaScript
// Display the alphabets from A to Z
process.stdout.write("The Alphabets from A to Z are: \n");
for (let i = 65; i <= 90; i++) {
    process.stdout.write(String.fromCharCode(i) + " ");
}

// Display the alphabets from a to z
process.stdout.write("\nThe Alphabets from a to z are: \n");
for (let i = 97; i <= 122; i++) {
    process.stdout.write(String.fromCharCode(i) + " ");
}

Output
The Alphabets from A to Z are: 
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 
The Alphabets from a to z are: 
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 

Explanation: The while loop continues as long as the character is within the required range. The i++ statement moves to the next alphabet after every iteration.

4. Print Alphabets Using a do-while Loop

A do-while loop executes its body at least once before checking the condition. It can also be used to print alphabets from A to Z and a to z.

C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
    // Declare the variables
    char i;

    // Display the alphabets
    cout << "The Alphabets from A to Z are: \n";

    // Traverse each character
    // with the help of while loop
    i = 'A';

    do{
        cout << i << ' ';
        i++;
    }while (i <= 'Z');
    // Display the alphabets
    i = 'a';

    cout << "\nThe Alphabets from a to z are: \n";

    do{
        cout << i << ' ';
        i++;
    }while (i <= 'z');

    return 0;
}
C
#include <stdio.h>

int main()
{
    // Declaring the variable
    char i;

    // Display the alphabets
    printf("The Alphabets from A to Z are: \n");

    // Traversing each character
    // with the help of do while loop

    i = 'A';

    do {
        printf("%c ", i);
        i++;
    } while (i <= 'Z');

    // for lower case alphabets
    printf("\nThe Alphabets from a to z are: \n");

    i = 'a';

    do {
        printf("%c ", i);
        i++;
    } while (i <= 'z');

    return 0;
}
Java
public class AlphabetDisplay {
    public static void main(String[] args) {
        // Declare the character variable 'i'
        char i;

        // Display the uppercase alphabets
        System.out.println("The Alphabets from A to Z are:");

        // Initialize 'i' with 'A' and use a do-while loop to print characters from 'A' to 'Z'
        i = 'A';
        do {
            System.out.print(i + " ");
            i++;
        } while (i <= 'Z');

        // Display the lowercase alphabets
        System.out.println("\nThe Alphabets from a to z are:");

        // Initialize 'i' with 'a' and use a do-while loop to print characters from 'a' to 'z'
        i = 'a';
        do {
            System.out.print(i + " ");
            i++;
        } while (i <= 'z');
    }
}
Python
# Display the alphabets from A to Z
print("The Alphabets from A to Z are: ")

# Initialize the character variable
i = 'A'

# Use a do-while loop to traverse and print the uppercase alphabets
while True:
    print(i, end=' ')
    i = chr(ord(i) + 1)
    if i > 'Z':
        break

# Display the alphabets from a to z
print("\nThe Alphabets from a to z are: ")

# Reset the character variable to 'a'
i = 'a'

# Use a do-while loop to traverse and print the lowercase alphabets
while True:
    print(i, end=' ')
    i = chr(ord(i) + 1)
    if i > 'z':
        break
C#
using System;

class Program
{
    static void Main()
    {
        // Display the alphabets from A to Z
        Console.WriteLine("The Alphabets from A to Z are:");

        // Traverse each character with the help of while loop
        char i = 'A';
        do
        {
            Console.Write(i + " ");
            i++;
        } while (i <= 'Z');

        // Display a new line
        Console.WriteLine();

        // Display the alphabets from a to z
        Console.WriteLine("The Alphabets from a to z are:");

        // Reset the variable
        i = 'a';

        // Traverse each character with the help of while loop
        do
        {
            Console.Write(i + " ");
            i++;
        } while (i <= 'z');

        // Display a new line
        Console.WriteLine();
    }
}
JavaScript
// Declare the variable 'i' for characters
let i;

// Display the uppercase alphabets
console.log("The Alphabets from A to Z are:");

// Initialize 'i' with 'A' and use a do-while loop to print characters from 'A' to 'Z'
i = 'A';
do {
    process.stdout.write(i + " ");
    i = String.fromCharCode(i.charCodeAt(0) + 1);
} while (i <= 'Z');

// Display the lowercase alphabets
console.log("\nThe Alphabets from a to z are:");

// Initialize 'i' with 'a' and use a do-while loop to print characters from 'a' to 'z'
i = 'a';
do {
    process.stdout.write(i + " ");
    i = String.fromCharCode(i.charCodeAt(0) + 1);
} while (i <= 'z');

Output
The Alphabets from A to Z are: 
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 
The Alphabets from a to z are: 
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 

Explanation: The do-while loop prints the current character and increments it. The condition is checked after each iteration, so the loop continues until the character goes beyond 'Z' or 'z'.

Comment