In this article, we are going to create a simple C program to create a simple pattern. Writing programs for creating patterns can be a little bit tricky, but with the right logic and the basic knowledge of C language, you can easily write programs for any pattern.
C program pattern (1 1 1 1 1 4 4 4 4 4 9 9 9 9 9 16 16 16 16 16 25 25 25 25 25)
We are going to write a program to create the following pattern –
1 1 1 1 1
4 4 4 4 4
9 9 9 9 9
16 16 16 16 16
25 25 25 25 25
As you can see, the pattern looks very simple. For simplicity, we have kept the number of rows and columns as same. If you observe every row, we have 5 elements, and on every row, the square of row number, like on the 3rd row, we have 9, which is the square of 3.
In the given video, you can find a detailed explanation along with the program to create this pattern.
Here is the C program to create a given pattern –
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= 5; j++) {
printf("%d ", i * i);
}
printf("n");
}
return 0;
}
Output Case 1 –
Enter the number of rows: 5
1 1 1 1 1
4 4 4 4 4
9 9 9 9 9
16 16 16 16 16
25 25 25 25 25
Output Case 2 –
Enter the number of rows: 7
1 1 1 1 1
4 4 4 4 4
9 9 9 9 9
16 16 16 16 16
25 25 25 25 25
36 36 36 36 36
49 49 49 49 49