0% found this document useful (0 votes)
7 views

C_Patterns_Examples

The document contains a series of C functions that generate various number patterns, including a number space pattern, square number pattern, multiplication table, rhombus pattern, alternating number pattern, pyramid with row numbers, continuous number pattern, X pattern, Z pattern, and a number diamond with row count. Each function includes a description and example output illustrating the resulting pattern. The patterns vary in complexity and visual structure, showcasing different programming techniques for generating formatted output.

Uploaded by

asfcsharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

C_Patterns_Examples

The document contains a series of C functions that generate various number patterns, including a number space pattern, square number pattern, multiplication table, rhombus pattern, alternating number pattern, pyramid with row numbers, continuous number pattern, X pattern, Z pattern, and a number diamond with row count. Each function includes a description and example output illustrating the resulting pattern. The patterns vary in complexity and visual structure, showcasing different programming techniques for generating formatted output.

Uploaded by

asfcsharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

6.

Number Pattern with Spaces


-----------------------------
void numberSpacePattern() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) printf(" ");
for (int j = 1; j <= i; j++) printf("%d", j);
for (int j = i - 1; j >= 1; j--) printf("%d", j);
printf("\n");
}
}
Output:
1
121
12321
1234321
123454321

7. Square Number Pattern


------------------------
void squareNumberPattern() {
int n = 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == 1 || i == n || j == 1 || j == n)
printf("%d ", n);
else
printf("%d ", n - 1);
}
printf("\n");
}
}
Output:
4 4 4 4
4 3 3 4
4 3 3 4
4 4 4 4

8. Multiplication Table Pattern


-------------------------------
void multiplicationPattern() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
printf("%3d ", i * j);
}
printf("\n");
}
}
Output:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
9. Rhombus Pattern
------------------
void rhombusPattern() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) printf(" ");
for (int j = 1; j <= n; j++) printf("%d ", i);
printf("\n");
}
}
Output:
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

10. Alternating Number Pattern


------------------------------
void alternatingNumberPattern() {
int n = 5, num = 1;
for (int i = 1; i <= n; i++) {
if (i % 2 == 1) {
for (int j = 1; j <= i; j++) printf("%d ", num++);
} else {
num = num + i - 1;
int temp = num;
for (int j = 1; j <= i; j++) printf("%d ", temp--);
num = num + i + 1;
}
printf("\n");
}
}
Output:
1
2 1
3 4 5
10 9 8 7
11 12 13 14 15

11. Pyramid with Row Numbers


----------------------------
void rowNumberPyramid() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) printf(" ");
for (int j = 1; j <= i; j++) printf("%d ", i);
printf("\n");
}
}
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

12. Continuous Number Pattern


-----------------------------
void continuousNumberPattern() {
int n = 4, num = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%2d ", num++);
}
printf("\n");
}
}
Output:
1
2 3
4 5 6
7 8 9 10

13. X Pattern
-------------
void xPattern() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j == i || j == n - i + 1) printf("*");
else printf(" ");
}
printf("\n");
}
}
Output:
* *
* *
*
* *
* *

14. Z Pattern
-------------
void zPattern() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == 1 || i == n || j == n - i + 1) printf("*");
else printf(" ");
}
printf("\n");
}
}
Output:
*****
*
*
*
*****

15. Number Diamond with Row Count


---------------------------------
void numberDiamondRowCount() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) printf(" ");
for (int j = 1; j <= 2 * i - 1; j++) printf("%d", i);
printf("\n");
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) printf(" ");
for (int j = 1; j <= 2 * i - 1; j++) printf("%d", i);
printf("\n");
}
}
Output:
1
222
33333
4444444
555555555
4444444
33333
222
1

You might also like