Pattern Programs in C | Half Pyramid

In this post, we learn about the pattern programs in c language which is very useful for exams, interviews as well as increase programming logic.
So let’s start,

What You Should Know?

To understand this example, you should know about the following C programming topics:

  • what is for loop in c programming.
  • what is nested for loop in c programming.
  • what is use of <stdio.h>.
  • what is main() function in c programming.

we dividedĀ  Pattern Program into three category

  • Basic Pattern Program(use two for loop)
  • Medium Pattern Program(ues two for loop reverse format)
  • Good Pattern Program(use three for loop)

1) Basic Pattern Programs in c

In Basic Pattern Program ,we use Two for loop

First for loop used for ROW
And
Second for loop used for COLUMN

Horizontal= Row AND Vertical=Column

Example-1

#include<stdio.h>
int main() {
   int row, column, total_rows;
   printf("Enter number of rows:");
   scanf("%d", &total_rows);
   for (row = 1; row  <= total_rows; row++) {
      for (column = 1; column  <= row; column++) {
         printf("* ");
      }
      printf("\n");
   }
   return 0;
}

Output

*
* *
* * *
* * * *
* * * * *

Example-2

#include<stdio.h>
int main() {
   int row, column, total_rows;
   printf("Enter number of rows: ");
   scanf("%d", &total_rows);
   for (row = 1; row  <= total_rows; row++) {
      for (column = 1; column <= row; column++) {
         printf("%d ", row);
      }
      printf("\n"); //it is used for print text next line 
   }
   return 0;
}

Output

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Example-3

#include<stdio.h>

int main() {
   int row, column, total_rows;
   printf("Enter number of rows: ");
   scanf("%d", &total_rows);
   for (row = 1; row <= total_rows; row++) {
      for (column = 1; column <= row; column++) {
         printf("%d ", column);
      }
      printf("\n");
   }
   return 0;
}

Output

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Example-4

#include<stdio.h>

int main() {

   int row, column, total_rows;
   int x = 1;
   printf("Enter number of rows: ");
   scanf("%d", &total_rows);
   for (row = 1; row <= total_rows; row++) {
      for (column = 1; column <= row; column++) {
         printf("%d ", x);
         x++;
      }
      printf("\n");
   }
   return 0;

}

Output

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Example-5

#include<stdio.h>
int main() {
   int row, column, total_rows;
   printf("Enter number of rows: ");
   scanf("%d", &total_rows);
   for (row = 1; row <= total_rows; row++) {
      for (column = 1; column <= total_rows; column++) {
         if (((row + column) % 2) == 0) { // Decides on as to which digit to print.
            printf("0 ");
         } else {
            printf("1 ");
         }
      }
      printf("\n");
   }
   return 0;
}

Output

0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0

Example-6

#include<stdio.h>
int main() {
   int row, column, total_rows;
   printf("Enter number of rows: ");
   scanf("%d", &total_rows);
   for (row = 1; row <= total_rows; row++) {
      for (column = 1; column <= total_rows; column++) {
         if (((row + column) % 2) == 0) { // Decides on as to which digit to print.
            printf("1 ");
         } else {
            printf("0 ");
         }
      }
      printf("\n");
   }
   return 0;
}

Output

1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1

Leave a Reply

avatar
  Subscribe  
Notify of