Hello, viewers welcome to my new blog on pattern printing in ‘C’ language with alphabets. So, let's start.........
Before going into the coding part let us have a look at the pattern that we want to print in ‘C’.
Printing Pattern of Pyramid with Alphabets |
So, today we will print the above pattern in ‘C’ and as we can see that the printing pattern is based on alphabetical order, so we need some knowledge about ASCII values. We will discuss it meanwhile.
We can see the printing pattern is formed of alphabets but
there’s an interesting thing happens that in every line or rows the alphabets become
printed in their sequential order but after the printing of alphabet in the middle
part of a line, the alphabets then started to print in opposite order i.e. back
counting order. For example, if we take the line or row no:3, then we can see
that up to the middle of the line, alphabets are printed in their sequential
order i.e ABC. But when we proceed further, it is seen that alphabets have become
started to print in opposite sequence i.e. previous-ABC + now- BA (ABCBA). So,
this is an important part of this printing pattern.
Now, let's see what is ASCII values?
In simple words, it is nothing but the converted integer
values of character, strings or some special character by which compiler
understands the respective character or string etc.
For upper case letters , ASCII value starts from 65 to 90
(A==65 ,B==66,C==67 to Z==90) and for lower case letters, ASCII values starts
from 97 to 122 (a==97,b==98,c==99 to z==122).
It will be more clear if we take the following example.....
if we write a print function: int
x=65;
printf(“%d”,x);
then the output will be: 65
but if we change the format
specifier from %d to %c, it will print its corresponding ASCII character.
i.e. printf(“%c”,x);
output: A
Hope you understand. Now, lets
take a look at our programming part.
- Outer "For Loop":-We have used here a total of 4 ‘for loops’ (1 outer and 3 inner ‘for loops’). For first ‘for loop’ we have made a condition that will make the loop iterate for ‘n’ times which means it will print ‘n’ number of lines or rows according to the input of the user. int i,j,k,n;
printf("enter the value of n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
- Nested "For Loop":-
- First 'Inner Loop':
Then, the first inner’ for loop’
begins with a condition of printing spaces up to (n-i) times for each iteration
of the outer loop. So, when the value of i=1,n=4 the loop will print
(n-i)=(4-1)=3 spaces for first row or line,when i=2,n=4 the loop will print
4-2=2 spaces for second-row or line and so on.
printf("enter the value of n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
- Second 'Inner Loop':
for(k=1;k<=i;k++)
{
printf("%c",k+64);
}
- Third 'Inner Loop':
for(k=i-1;k>0;k--)
{
printf("%c",k+64);
}
Thus, the entire “Nested For
Loop” goes on.
NOTE: In the print section, we have
added 64 with the value of ‘k’ to get the ASCII values of upper case alphabet(when
k=1,k+64=65==A; when k=2,k+64=66==B). If we want to get lower case alphabet
then, we need to 96 with ‘k’ as lower case letters start from ASCII values of
97.
Thus, the entire Programming Code
is as follows:
Hope you understand the printing
pattern and programming code. Please share and comment if this helps you. Thank
you very much.
No comments:
Post a Comment