In this tutorial you will learn about the C program to Print Box Number Pattern of 1 and 0 and its application with practical example.
C program to Print Box Number Pattern of 1 and 0
In this tutorial, we will learn to create a C program that will Print a Box Number Pattern of 1 and 0 in C programming
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
What is the box?
The box can be square or rectangle in shape. We can make shapes in the C programming language. The Box will be filled with numbers, today’s tutorial.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declaring the required variables for the program. 2. Taking the input size of the sides of the box from the user. 3. Generating the pattern using the nested for loop. 4. Printing the box square or rectangle pattern. 5. End the program. |
Program description Printing a Box:-
In this program, we will first take the input sides of the box in the form of rows and columns. Then we will use nested for loops for the program to print the box of required rows and columns. We will print the box with numbers “0” & “1” only for the shape.
The below is an example of the square program code.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include <stdio.h> int main() { //declaring the required variable for the program. int rw, cl, i, j; int ctrrw, ctrcl; /* i = it will hold the integer value for the program */ /* j = it will hold the integer value for the program */ /* rw = it will hold the integer value for the program */ /* cl = it will hold the integer value for the program */ /* ctrrw = it will hold the integer value for the program */ /* Taking the Input number of rows from user */ printf("Enter number of rows: "); scanf("%d", &rw); printf("Enter number of columns: "); scanf("%d", &cl); /* Finding the center row and column */ ctrrw = (rw + 1) / 2; ctrcl = (cl + 1) / 2; for(i=1; i<=rw; i++) { for(j=1; j<=cl; j++) { if(ctrcl == j && ctrrw == i) { printf("0"); } else if(cl%2 == 0 && ctrcl+1 == j) { /* printig the output of the program. */ if(ctrrw == i || (rw%2 == 0 && ctrrw+1 == i)) printf("0"); else printf("1"); } else if(rw%2 == 0 && ctrrw+1 == i) { /* printig the output of the program. */ if(ctrcl == j || (cl%2 == 0 && ctrcl+1 == j)) printf("0"); else printf("1"); } else { printf("1"); } } printf("\n"); } return 0; } |
Output:-
In the above program, we have last initialized the required variable.
- rw = it will hold the integer value.
- cl = it will hold the integer value.
- j = it will hold the integer value.
- i = it will hold the integer value.
Input the number of rows for the pattern.
Code to make the pattern on the screen.
Printing output pattern using C programming.