HomeC++C++ Program to Display Characters from A to Z Using Loop

C++ Program to Display Characters from A to Z Using Loop

In this post, you will learn how to Display Characters from A to Z Using Loop in C++.

This lesson will teach you how to display characters from A to Z, with a for loop using the C++ Language. Let’s look at the below source code.

How to Display Characters from A to Z Using Loop?

RUN CODE SNIPPET

Source Code

#include <iostream> 
using namespace std; 
int main() 
{ 
    char i; 
    cout << "The Alphabets from A to Z are \n"; 
    for (i = 'A'; i <= 'Z'; i++) 
    { 
        cout << i <<" ";
    } 
    cout<<endl;
    char j; 
    cout<<"\n" << "The Alphabets from a to z are \n"; 
    for (j = 'a'; j <= 'z'; j++) 
    { 
        cout << j <<" "; 
    }
    return 0; 
}

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

The statements #include<iostream>, using namespace std, int main are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.

  1. Declare the variable i as a character, and display the output statement using cout<< and the Insertion Operators'<<‘ .
  2. Declare the for loop with the condition for (i = 'A'; i <= 'Z'; i++) . The condition assigns i = ‘A’, checks if the value of i is less than or equal to ‘Z’ then increments the i.
  3. The for loop is executed many times until the condition is not satisfied. When the condition is satisfied the condition statement is displayed.
  4. The statement cout<<endl; ends the line so that the following statement starts from the next line.
  5. Similarly to display the small letter of the alphabet we create a variable  j as a character and declare the for loop condition replacing the capital letter with small letters, and the loop statement.

Note: The ‘ << endl ‘ in the code is used to end the current line and move to the next line and ‘\n’ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.

Additional Source Code

You can also try the source code given below, the following code is structured using do while loop and a increment operator. Let’s look at the below source code.

RUN CODE SNIPPET
#include<iostream>
using namespace std;
int main()
{
    char alphabet='A';
    cout<<"The Alphabets from A to Z are\n"<<endl;
    do
    {
        cout<<alphabet<<"\n";
        alphabet++;
    }
    while(alphabet<='Z');
    cout<<endl;
    return 0;
}

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
  1. We declare the string alphabet as a character with char data type and assign the alphabet A.
  2. The do while loop is different from the for loop as the condition is seen after the loop statement. The loop statement is executed once before the condition is verified.
  3. Then if the condition is true then, the loop is executed multiple times until the condition is false. The execution is terminated, when the condition is false.
  4. The statement cout<<endl; ends the line so that the following statement starts from the next line.
  5. Here we are displaying the answer vertically by adding ‘\n’ in the output statement.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
  • C++
  • January 30, 2022