Powered By Blogger

Sunday 15 March 2020

Pyramid printing pattern in 'C' programming with ALPHABETS


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 in 'C' programming
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.

  1. 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++)
    {
  2. 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.

for(j=1;j<=n-i;j++)

{
printf(" ");
}
  • Second 'Inner Loop':  
         For the second inner ‘for loop’, there’s the condition for alphabet printing in sequential order up to ‘i’ times for each time the outer loop iterates. When the value of i=1, it prints only one alphabet(A), when i=2, it prints two alphabets(AB), when i=3 it prints three alphabets (ABC) in sequential order and so on. Thus, the second inner ‘for loop’ completes its iteration.


for(k=1;k<=i;k++)
{
    printf("%c",k+64);
}
  • Third 'Inner Loop':
          For the third inner ‘for loop’, we need to remember after the iteration of 2nd inner for loop, the 3rd inner loop immediately starts to print alphabet in opposite order i.e.(if the 2nd loop prints “ABCD” then the 3rd loop will print “CBA”). So, the condition is given when k=i-1 and k>0. We can see, when i=1,k becomes k=(i-1)=1-1=0, so it will not print any alphabet, when i=2, k=2-1=1, so it will print one alphabet(i.e. A), when i=3, k=3-1=2, so it will print two alphabets(i.e. BA) but in the opposite order, as 'k' is given as a decrement operator(k--) and check the condition until K>0. 


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

Can we use pointers without specifying the data type of the pointer?

Void Pointer Yes, we can use pointers without specifying the data type of the pointers. But remember at the end of the programming, wh...