Introduction :
In this tutorial, we will learn how to print the ASCII values of all English alphabets in upper case and lower case format. With this program, you will learn how to find out the ASCII value of a character in C++.
Algorithm :
We will use two for loops to iterate through the upper case and lower case characters. Inside the loops, we will print the ASCII values of each character. So, our algorithm is like below :
-
Iterate through the uppercase characters one by one using a loop.
-
Print out the ASCII value for each character.
-
Iterate through the lowercase characters one by one using a loop.
-
Print out the ASCII value for each character.
Let’s write these steps in code :
C++ program :
#include <iostream>
using namespace std;
int main()
{
char c;
cout << "Upper case : " << endl;
for (c = 'A'; c <= 'Z'; c++)
{
cout << c << " : " << (int)c << endl;
}
cout << "Lower case : " << endl;
for (c = 'a'; c <= 'z'; c++)
{
cout << c << " : " << (int)c << endl;
}
return 0;
}
Explanation :
As you can see here,
-
We can iterate through the characters of the alphabet using a for loop like iterating through the numbers.
-
(int)c returns the ASCII value of a character c.
Output :
It will print the below output :
Upper case :
A : 65
B : 66
C : 67
D : 68
E : 69
F : 70
G : 71
H : 72
I : 73
J : 74
K : 75
L : 76
M : 77
N : 78
O : 79
P : 80
Q : 81
R : 82
S : 83
T : 84
U : 85
V : 86
W : 87
X : 88
Y : 89
Z : 90
Lower case :
a : 97
b : 98
c : 99
d : 100
e : 101
f : 102
g : 103
h : 104
i : 105
j : 106
k : 107
l : 108
m : 109
n : 110
o : 111
p : 112
q : 113
r : 114
s : 115
t : 116
u : 117
v : 118
w : 119
x : 120
y : 121
z : 122