C program to print box pattern using a number or any other character

C program to print box pattern using a number or any other character:

Let’s learn how to print a box pattern in C. The program will take the size of the box as an input from the user and it will print that box on the console.

We can use numbers or any other character to print the box pattern. In this post, I will show you how to print it using numbers and using any given character.

Algorithm to print a box pattern using a character:

Let’s say the size of the box is 5. If we use * to print the box, it will look as like below:

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

To print this pattern, we can use two loops.

  • Both loops will run for n number of times, where n is the size of the box.
  • The outer loop will point to the row on each iteration, i.e. for the first iteration, it will point to the first row, for the second iteration, it will point to the second row etc.
  • The inner loop will print the row content. For each iteration of the outer loop, the inner loop will run for n number of times and it will print the contents of the row i.e. *.
  • We can also take the character to print this pattern as an input from the user.

Let’s write down the program to print this pattern.

Example 1: C program to print a box pattern using a character.

Below is the complete C program that uses * to print the box pattern:

#include <stdio.h>

int main()
{
    int i, j, size;

    printf("Enter the size of the pattern: ");
    scanf("%d", &size);

    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size; j++)
        {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}

Here,

  • i and j are two integer variables to use in the for loops. The size is another integer variable to hold the size.
  • It asks the user to enter the size. It reads that value and stores in the size variable.
  • The outer and inner for loops runs for n number of times.
  • Inside the inner loop, it prints a * with a trailing space.
  • Once an iteration of the inner loop completes, it prints a new line. This will move to the next line, i.e. a new line at the end of each line.

If you run this program, it will print output as like below:

Enter the size of the pattern: 5
* * * * * 
* * * * * 
* * * * * 
* * * * * 
* * * * * 

Example 2: C program to print a box pattern using a number:

This program is similar to the above program. Instead of using *, we can use any other number to print the box pattern.

Let’s take the number as an input from the user and print the box using the user given number:

#include <stdio.h>

int main()
{
    int i, j, size, n;

    printf("Enter the size of the pattern: ");
    scanf("%d", &size);

    printf("Enter the number to print the box: ");
    scanf("%d", &n);

    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size; j++)
        {
            printf("%d ", n);
        }
        printf("\n");
    }

    return 0;
}

This is the same program we used with *. The only difference is that it takes the number as an input from the user to print the box. The number is stored in the variable n and it prints that box with n.

If you run this program, it will give output as like below:

Enter the size of the pattern: 6
Enter the number to print the box: 0
0 0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 

Enter the size of the pattern: 7
Enter the number to print the box: 1
1 1 1 1 1 1 1 
1 1 1 1 1 1 1 
1 1 1 1 1 1 1 
1 1 1 1 1 1 1 
1 1 1 1 1 1 1 
1 1 1 1 1 1 1 
1 1 1 1 1 1 1 

Enter the size of the pattern: 6
Enter the number to print the box: 23
23 23 23 23 23 23 
23 23 23 23 23 23 
23 23 23 23 23 23 
23 23 23 23 23 23 
23 23 23 23 23 23 
23 23 23 23 23 23 

You can use any number to print the pattern.

Example 3: C program to print a box pattern using any character:

If you want to use a user-given character to print it, you can change the program as like below:

#include <stdio.h>

int main()
{
    int i, j, size;
    char c;

    printf("Enter the size of the pattern: ");
    scanf("%d", &size);
    getchar();

    printf("Enter the character to print the box: ");
    scanf("%c", &c);

    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size; j++)
        {
            printf("%c ", c);
        }
        printf("\n");
    }

    return 0;
}
  • The character variable c is used to hold the user input character.
  • It reads the character as an input from the user.
  • The getchar() is used to read the newline character because the user will hit enter after entering the size. Otherwise, it will read the enter to c.

It will give output as like below:

Enter the size of the pattern: 5
Enter the character to print the box: &
& & & & & 
& & & & & 
& & & & & 
& & & & & 
& & & & & 

Enter the size of the pattern: 6
Enter the character to print the box: ^
^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ 

Enter the size of the pattern: 4
Enter the character to print the box: %
% % % % 
% % % % 
% % % % 
% % % % 

You might also like: