Print square of upper-case char Horizontally|Vertically

C-Pattern-Programs

In this article, we are going to learn about how to print Character Patter in C Language using ASCII and maths numbers. Pattern programs are a good exercise to master the loops in the C language.


We will try to print below two types of patterns


  • Print square of upper-case char Horizontally.
  • Print square of upper-case char Vertically.

Print square pattern of upper-case characters Horizontally


In this example, we are printing a square box pattern by using upper case characters. We will print characters horizontally n number of times and then switch to the next line with a new character. The ASCII value of the Upper case characters starts at 65. ‘A’ has the value of 65 in ASCII.

Desired Output for Size 3:
A A A
B B B
C C C

Print square pattern of upper-case characters Horizontally

#include<stdio.h>
int main()
{
   int num;

   printf("Please enter the size of Square Box: ");
   scanf("%d",&num);

   for(int row=1; row<=num; row++)
   {
     for(int col=1; col<=num; col++)
     {
       printf("%3c",row+64);
     }
     printf("\n");
   }

   return 0;
}

Actual Output:

Please enter the size of Square Box: 5
  A  A  A  A  A
  B  B  B  B  B
  C  C  C  C  C
  D  D  D  D  D
  E  E  E  E  E

Print square pattern of upper-cases Vertically


In this example, we are printing square box pattern by using upper case characters. We will print characters Vertically n number of times.The ASCII value of the Upper case characters starts at 65. ‘A’ has the value of 65 in ASCII.

Desired Output for Size 3:
A B C
A B C
A B C
#include<stdio.h>
int main()
{
   int num;

   printf("Please enter the size of Square Box: ");
   scanf("%d", &num);

   for(int row=1; row<=num; row++)
   {
     for(int col=1; col<=num; col++)
     {
       printf("%3c",col+64);
     }
     printf("\n");
   }

   return 0;
}

Output

Please enter the size of Square Box: 5
  A  B  C  D  E
  A  B  C  D  E
  A  B  C  D  E
  A  B  C  D  E
  A  B  C  D  E

We can do the same pattern by using the lower case characters.The ASCII value of the Upper case characters starts at 97. ‘a’ has the value of 97 in ASCII.


#include<stdio.h>
int main()
{
   int num;

   printf("Please enter the size of Square Box: ");
   scanf("%d",&num);

   for(int row=1; row<=num; row++)
   {
     for(int col=1; col<=num; col++)
     {
       printf("%3c",col+96);
     }
     printf("\n");
   }

   return 0;
}

Output

Please enter the size of Square Box: 5
  a  b  c  d  e
  a  b  c  d  e
  a  b  c  d  e
  a  b  c  d  e
  a  b  c  d  e