You Should Know
To understand this example, you should know about the following C programming topics:
- pattern programs in c|part-2
- what is for loop in c programming.
- what is use of <stdio.h>.
- what is main() function in c programming
- what is scanf() and printf()
Pattern Programs in C
Example-1
#include<stdio.h>
int main() {
int row, column, total_rows, x;
printf("Enter number of rows: ");
scanf("%d", &total_rows);
x = 1;
for (row = total_rows; row >= 1; row--) {
for (column = 1; column <= row; column++) {
printf(" %d ", x);
}
printf("\n");
x++;
}
return 0;
}
Output
1 1 1 1 1
2 2 2 2
3 3 3
4 4
5
Example-2
#include<stdio.h>
int main() {
int row, column, total_rows, x;
printf("Enter number of rows: ");
scanf("%d", &total_rows);
for (row = total_rows; row >= 1; row--) {
x = total_rows;
for (column = 1; column <= row; column++) {
printf(" %d ", x);
x--;
}
printf("\n");
}
return 0;
}
Output
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
Example-3
#include<stdio.h>
int main() {
int i, row, column, total_rows;
int x = 0;
printf("Enter number of rows: ");
scanf("%d", &total_rows);
for (i = 1; i <= total_rows; i++) {
x += i; // same as x=x+i
}
for (row = total_rows; row >= 1; row--) {
for (column = 1; column <= row; column++) {
printf("%d ", x);
x--;
}
printf("\n");
}
return 0;
}
Output
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
Leave a Reply