Mastering Fundamentals: C++ and Data Structures

Displaying A to Z characters using a loop: C++ Program

Displaying A to Z characters using a loop: C++ Program

Introduction

In this article, we will be learning the approach to solve this problem of displaying characters from A to Z. Alphabets in lowercase and uppercase can be printed using two methods, first is using ASCII values and second is to directly print values from ā€˜Aā€™ to ā€˜Zā€™ using loops.

But for this we first need to know the ascii values of these:

  • ASCII value of Uppercase Alphabets: 65 to 90.
  • ASCII value of Lowercase Alphabets: 97 to 122.

Now we will discuss two approaches to accomplish this task:

Method 1: Using ASCII Value

#include <iostream>
using namespace std;
 
// Function to print the alphabet in lowercase
void lowercaseAlphabets()
{
    // lowercase
    for (int c = 97; c <= 122; ++c)
        cout << c << " ";
 
    cout << endl;
}
// Function to print the alphabet in upper case
void uppercaseAlphabets()
{
 
    // uppercase
    for (int c = 65; c <= 90; ++c)
        cout << c << " ";
 
    cout << endl;
}
int main()
{
 
    cout << "Uppercase Alphabets Are:" << endl;
    uppercaseAlphabets(ch);
 
    cout << "Lowercase Alphabets Are: " << endl;
    lowercaseAlphabets(ch);
 
    return 0;
}

Output:

Uppercase Alphabets
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 


Lowercase Alphabets
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

write your code here: Coding Playground

Method 2: Using Alphabets Directly

#include <iostream>
using namespace std;
 
// Function to print the alphabet
// in lower case
void lowercaseAlphabets()
{
    // lowercase
 
    for (char c = 'a'; c <= 'z'; ++c)
        cout << c << " ";
 
    cout << endl;
}
// Function to print the alphabet
// in upper case
void uppercaseAlphabets()
{
 
    // uppercase
    for (char c = 'A'; c <= 'Z'; ++c)
        cout << c << " ";
 
    cout << endl;
}
 
// Driver code
int main()
{
 
    cout << "Uppercase Alphabets" << endl;
    uppercaseAlphabets(ch);
 
    cout << "Lowercase Alphabets " << endl;
    lowercaseAlphabets(ch);
 
    return 0;
}

Output:

Uppercase Alphabets
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 


Lowercase Alphabets
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

write your code here: Coding Playground

So, this sums up the article.We have discussed two approaches here, and I hope you must be clear with these both approaches.Do share this article with your friends and keep learning.Happy Learning!!