C++ program to print alphabets from a to z

In this program, we will learn to print all the alphabets using a loop. It should be noted that we can execute a loop on characters too. In such a case the loop runs on the ASCII values of the characters.

See also: C program to Find ASCII Value of a Character

Example 1

In this example, we will print the alphabets in uppercase.

#include <iostream> 
using namespace std; 
int main() 
{ 
	char i; 
	for(i = 'A'; i <= 'B'; i++)
	{
		cout<<i;
	} 
	return 0;
}

Output:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Explanation

We have used a for loop in the above example which runs from A to Z. As I said earlier, in the case we run a loop on characters, the loop runs on their ASCII values. So, the loop will run from 65 to 90. (As the ASCII value of A is 65 and that of Z is 90). Inside the loop, we are printing the current value of i.

Example 2

In this example, we are printing the alphabets in lowercase.

#include <iostream>
using namespace std;
int main()
{
    for(char i = 'a';i <= 'b'; i++){
         cout<<i;
    }
    return 0;
}

Output:

abcdefghijklmnopqrstuvwxyz

Example 3

In this example, we are asking the user whether the alphabets should be printed in uppercase or lowercase.

#include <iostream>
using namespace std;
int main()
{
    char i, choice;
    
    cout<<"Enter your choice. L for lowercase and U for uppercase: ";
    cin>>choice;
    
    if(choice == 'L' || choice == 'l')
    {
        for(i = 'a';i <= 'z'; i++)
        {
            cout<<i;
        }
    }
    else
    {
        for(i = 'A';i <= 'Z'; i++){
            cout<<i;
        }
    }
    
    return 0;
}

Output:

Enter your choice. L for lowercase and U for uppercase: l

abcdefghijklmnopqrstuvwxyz