C program to print circle box number pattern with 1 and 0

Write a C program to print the given circle number pattern with 1's and 0's. How to print circle number pattern of one's and zero's using for loop in C programming. Logic to print the box number pattern with 1 as border and 0 in center and corners.

Example:
Input rows: 5
Input cols: 5
Output:
01110
10001
10001
10001
01110


Required knowledge

Basic C programming, Loop

Logic to print circle box number pattern

Before you move on to this number pattern I recommend you to learn one of the similar number pattern. As this program is almost similar to the above mentioned pattern.

Talking about this pattern, it consists of following noticeable things:
  1. Zeros are printed in center and at the corners.
  2. Ones are printed at the edges.
Now, after looking on to these constraints you can easily formulate the logic of the given pattern. Before we print anything in the inner loop we need to check three conditions. First, if it is the corner element i.e. if it is first row and column, or first row last column, or last row first column, or last row and column then print 0. Second, if it is first or last row or first or last column then print 1. Otherwise for every occurrence print 0.

Program to print circle box number pattern

/**
 * C program to print circle box number pattern
 */

#include <stdio.h>

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

    printf("Enter rows: ");
    scanf("%d", &N);

    for(i=1; i<=N; i++)
    {
        for(j=1; j<=N; j++)
        {
            //Print corner elements
            if((i==1 || i==N) && (j==1 || j==N))
            {
                printf("0");
            }
            else if(i==1 || i==N || j==1 || j==N)
            {
                //Print edges
                printf("1");
            }
            else
            {
                //Print center
                printf("0");
            }
        }

        printf("\n");
    }

    return 0;
}


Output
Enter rows: 5
01110
10001
10001
10001
01110


Snapshot

C program to print circle box number pattern with 1 and 0


Now once you are done with the above pattern you can easily play with the inner printf() statements to get following cool patterns of 0's and 1's.
10001
01110
01110
01110
10001
 111
1   1
1   1
1   1
 111
 000
0   0
0   0
0   0
 000
1   1
 111
 111
 111
1   1
0   0
 000
 000
 000
0   0

Happy coding ;)


You may also like

Labels: , , ,