C Program to Display Characters from A to Z Using Loop

Displaying characters from A to Z using a loop is a common programming exercise that helps understand iteration and character handling in C. This article will cover various C Program to Display Characters from A to Z Using Loop. We will explore multiple examples, each demonstrating a different approach to solve the problem.

Prerequisites

Before we delve into the examples, ensure you have the following prerequisites:

  • A C compiler (such as GCC)
  • A text editor or IDE for writing your C code
  • Basic understanding of C programming concepts, especially loops and character handling

1. Displaying Characters from A to Z

In this section, we will look at several methods to display characters from A to Z in C.

1.1 Using a for Loop

Example 1: Display Characters Using a for Loop

This method uses a for loop to iterate through the characters from A to Z.

Code

C
#include <stdio.h>

int main() {
    char ch;

    printf("Characters from A to Z:\n");
    for (ch = 'A'; ch <= 'Z'; ++ch) {
        printf("%c ", ch);
    }

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Iterate using a for loop: The loop runs from ‘A’ to ‘Z’, printing each character.
  • Output the result: printf displays each character.

Output

C
Characters 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 

1.2 Using a while Loop

Example 2: Display Characters Using a while Loop

This method uses a while loop to iterate through the characters from A to Z.

Code

C
#include <stdio.h>

int main() {
    char ch = 'A';

    printf("Characters from A to Z:\n");
    while (ch <= 'Z') {
        printf("%c ", ch);
        ++ch;
    }

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Iterate using a while loop: The loop runs from ‘A’ to ‘Z’, printing each character.
  • Output the result: printf displays each character.

Output

C
Characters 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 

1.3 Using a do-while Loop

Example 3: Display Characters Using a do-while Loop

This method uses a do-while loop to iterate through the characters from A to Z.

Code

C
#include <stdio.h>

int main() {
    char ch = 'A';

    printf("Characters from A to Z:\n");
    do {
        printf("%c ", ch);
        ++ch;
    } while (ch <= 'Z');

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Iterate using a do-while loop: The loop runs from ‘A’ to ‘Z’, printing each character.
  • Output the result: printf displays each character.

Output

C
Characters 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 

1.4 Using ASCII Values

Example 4: Display Characters Using ASCII Values

This method uses ASCII values to iterate through the characters from A to Z.

Code

C
#include <stdio.h>

int main() {
    int i;

    printf("Characters from A to Z:\n");
    for (i = 65; i <= 90; ++i) {
        printf("%c ", i);
    }

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Iterate using a for loop: The loop runs from 65 to 90, which are the ASCII values for ‘A’ to ‘Z’.
  • Output the result: printf displays each character.

Output

C
Characters 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 

1.5 Using Arrays

Example 5: Display Characters Using Arrays

This method stores the characters in an array and prints them.

Code

C
#include <stdio.h>

int main() {
    char alphabet[26];
    int i;

    for (i = 0; i < 26; ++i) {
        alphabet[i] = 'A' + i;
    }

    printf("Characters from A to Z:\n");
    for (i = 0; i < 26; ++i) {
        printf("%c ", alphabet[i]);
    }

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Store characters in an array: Use a loop to fill the array with characters from ‘A’ to ‘Z’.
  • Print the characters: Use a loop to print the characters from the array.
  • Output the result: printf displays each character.

Output

C
Characters 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 

2. Conclusion

In this article, we explored various methods to display characters from A to Z in C: using a for loop, using a while loop, using a do-while loop, using ASCII values, and using arrays. Each method demonstrates different aspects of handling loops and character manipulation in C programming. By understanding these methods, you can choose the one that best fits your specific needs and enhance your skills in basic programming logic and character handling in C.