Pattern Programs in C | Half Pyramid | Part 2

In This Post, we learn about Pattern Programs in c which is half pyramid pattern.

This Post is the same as part-1 but the difference is reverse of part-1 means the Pattern start from high or max numbers to lower or decrease number while Part-1 start from a lower number to a higher number.

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.

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 = total_rows;
   for (row = 1; row <= total_rows; row++) {
      for (column = 1; column <= row; column++) {
         printf("%d ", x);
      }
      x--;
      printf("\n");
   }
   return 0;

}

Output

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

Example-2

#include<stdio.h>

int main() {

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

      printf("\n");
   }
   return 0;
}

Output

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

Example-3

#include<stdio.h>

int main() {

   int i, row, column, total_rows, x;

   printf("Enter number of rows: ");
   scanf("%d", &total_rows);
   x = 0;
//use this for loop  to count max number
   for (i = 1; i <= total_rows; i++) {
      x += i; 
   }

   for (row = 1; row <= total_rows; row++) {

      for (column = 1; column <= row; column++) {
         printf("%d ", x);
         x--;
      }

      printf("\n");
   }
   return 0;
}

Example-3.1(another method)

#include<stdio.h>

int main() {

   int i, row, column, total_rows, x;

   printf("Enter number of rows: ");
   scanf("%d", &total_rows);
   //n(n+1)/2  we use this formula to count max number
  //here n=total_rows
   x = (total_rows*(total_rows+1))/2;
   for (row = 1; row <= total_rows; 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

avatar
  Subscribe  
Notify of