C++ Program to Display Letters from A to Z

Characters from A to Z

C++ programs to display uppercase letters from A to Z or lowercase letters from a to z have been shown here. Both the iterative and recursive approaches have been covered.







1. C++ program & output to Display Characters from A to Z




1.1. C++ program & output to Display Characters from A to Z using Iteration

Code has been copied
/*************************************
	   alphabetacoder.com
C++ Program to display characters 
from A to Z or a to z using iteration
**************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int i, s;

    // take input of the number
    cout << "Enter 1 to display A to Z" << endl;
    cout << "Enter 2 to display a to z" << endl;
    cout << "Your choice >> ";
    cin >> s;

    // display result
    if (s == 1) {
        // display uppercase letters
        for (i = 'A'; i <= 'Z'; i++) {
            cout << (char) i << " ";
        }
    } else if (s == 2) {
        // display lowercase letters
        for (i = 'a'; i <= 'z'; i++) {
            cout << (char) i << " ";
        }
    } else
        cout << "Wrong choice!" << endl;

    return 0;
}

Output


Case 1:

Enter 1 to display A to Z

Enter 2 to display a to z

Your choice >> 1

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


Case 2:

Enter 1 to display A to Z

Enter 2 to display a to z

Your choice >> 2

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


Case 3:

Enter 1 to display A to Z

Enter 2 to display a to z

Your choice >> 3

Wrong choice!




1.2. C++ program & output to Display Characters from A to Z using Recursion

Code has been copied
/**************************************
           alphabetacoder.com
 C++ Program to display characters 
 from A to Z or a to z using recursion
***************************************/

#include <iostream>

using namespace std;

// recursive function to print A to Z
void print_uppercase(char i, char j) {
    // display current character
    cout << i << " ";

    // exit condition
    if (i == j)
        return;
    // call function
    print_uppercase(++i, j);
}

// recursive function to print a to z
void print_lowercase(char i, char j) {
    // display current character
    cout << i << " ";

    // exit condition
    if (i == j)
        return;
    // call function
    print_lowercase(++i, j);
}

int main() {
    // declare variables
    int i, s;

    // take input of the number
    cout << "Enter 1 to display A to Z" << endl;
    cout << "Enter 2 to display a to z" << endl;
    cout << "Your choice >> ";
    cin >> s;

    // display result
    if (s == 1) {
        // display uppercase letters
        // by calling recursive function
        print_uppercase('A', 'Z');

    } else if (s == 2) {
        // display lowercase letters
        // by calling recursive function
        print_lowercase('a', 'z');
    } else
        cout << "Wrong choice!" << endl;

    return 0;
}

Output


Case 1:

Enter 1 to display A to Z

Enter 2 to display a to z

Your choice >> 1

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


Case 2:

Enter 1 to display A to Z

Enter 2 to display a to z

Your choice >> 2

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


Case 3:

Enter 1 to display A to Z

Enter 2 to display a to z

Your choice >> 0

Wrong choice!