Home C Programming Language / Programs / C program to print circle box number pattern with 1 and 0 01110 10001 10001 10001 01110
🚀 Programming Example

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

👁 2,872 Views
💻 Practical Program
📘 Step Learning
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.

💻 Program Code

/**
 * C program to print circle box number pattern
* www.atnyla.com
 */

#include <stdio.h>

int main()
{
    int i, j, rows, cols;

    /* Input rows and columns from user */
    printf("Enter rows: ");
    scanf("%d", &rows);
    printf("Enter columns: ");
    scanf("%d", &cols);

    for(i=1; i<=rows; i++)
    {
        for(j=1; j<=cols; j++)
        {
            // Print corner element
            if((i==1 || i==rows) && (j==1 || j==cols))
            {
                printf("0");
            }
            else if(i==1 || i==rows || j==1 || j==cols)
            {
                // Print edge
                printf("1");
            }
            else
            {
                // Print center
                printf("0");
            }
        }

        printf("\n");
    }

    return 0;
}
                        

🖥 Program Output

Enter rows: 5
Enter columns: 5
01110
10001
10001
10001
01110
                            

📘 Explanation

Required knowledge

Basic C programming, Loop

Logic to print circle box number pattern

The given pattern is almost similar to one of previous explained pattern -

 

11111
10001
10001
10001
11111

 

The only difference is zero gets printed for corner elements instead of 1.
Below is the step by step descriptive logic to print the given pattern.

  1. Input number of rows and columns to print from user. Store it in some variable say rows and cols.
  2. To iterate through rows run an outer loop from 1 to rows. The loop structure should look like for(i=1; i<=rows; i++).
  3. To iterate though columns run an inner loop from 1 to cols. The loop structure should look like for(j=1; j<=cols; j++).
  4. Inside the inner loop, print 1 for edges except for corner elements. Which is if(i==1 || i==rows || j==1 || j==rows) and not a corner then print 1 otherwise print 0.
  5. Finally, move to the next line after printing all columns of a row.
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.