Pages

Saturday, May 2, 2020

C Programs To Print Triangle, Pyramid, Pascal's Triangle, Floyd's Triangle for Freshers

1. Introduction


In this tutorial, You'll learn how to print the different types of patters in C programming language.

The following programs are shown in this article. These patterns can be Half or left or right or full pyramid patterns.


  • Half Pyramid Of Numbers
  • Half Pyramid Of stars(*)
  • Full Pyramid Of stars(*) or Other Symbol
  • Pascal's Triangle
  • Floyd's triangle


Read more on Koltin programs which is mostly used in android.

C Programs To Print Triangle, Pyramid, Pascal's Triangle, Floyd's Triangle




2. C Program to Print Half Pyramid Of Numbers


This program prints the numbers in the half pyramid format.

[#include <stdio.h>
int main() {
   int i, j, rows;
   printf("Enter the number of rows for pattern printing ");
   scanf("%d", &rows);
   for (i = 1; i <= rows; ++i) {
      for (j = 1; j <= i; ++j) {
         printf("%d ", j);
      }
      printf("\n");
   }
   return 0;
}]

Output:

[Enter the number of rows for pattern printing 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5]

3. C Program to Print Half Pyramid Of stars(*)


This program prints the asterisk character '*' in the half pyramid format. Here, no of rows is configured in global variable MAX.

[#include<stdio.h>

#define MAX 5
int main()
{
    int i,j;
    for(i=0; i< MAX; i++)
    {
        for(j=0;j<=i;j++)
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}]

Output:

 [*
 **
 ***
 ****
 *****]

4. C Program to Print Full Pyramid Of stars(*) or Other Symbol


The bellow c program prints the Full pyramid of numbers.


[#include <stdio.h>
int main() {
   int i, space, rows, k = 0;
   printf("Enter the number of rows: ");
   scanf("%d", &rows);
   for (i = 1; i <= rows; ++i, k = 0) {
      for (space = 1; space <= rows - i; ++space) {
         printf("  ");
      }
      while (k != 2 * i - 1) {
         printf("* ");
         ++k;
      }
      printf("\n");
   }
   return 0;
}]

Output:

[Enter the number of rows: 5
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *]

5. C Program to Print Pascal's Triangle


In Pascal's triangle, each number is the sum of the two numbers directly above it.




[#include <stdio.h>
int main() {
   int rows, coef = 1, space, i, j;
   printf("Enter the number of rows: ");
   scanf("%d", &rows);
   for (i = 0; i < rows; i++) {
      for (space = 1; space <= rows - i; space++)
         printf("  ");
      for (j = 0; j <= i; j++) {
         if (j == 0 || i == 0)
            coef = 1;
         else
            coef = coef * (i - j + 1) / j;
         printf("%4d", coef);
      }
      printf("\n");
   }
   return 0;
}]

Output:

[Enter the number of rows: 5
           1
         1   1
       1   2   1
     1   3   3    1
   1  4    6   4   1
 1  5   10   10  5   1]


6. C Program to Print Floyd's triangle


Floyd's triangle is a right-angled triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner:

[#include <stdio.h>
int main() {
   int rows, i, j, number = 1;
   printf("Enter the number of rows: ");
   scanf("%d", &rows);
   for (i = 1; i <= rows; i++) {
      for (j = 1; j <= i; ++j) {
         printf("%d ", number);
         ++number;
      }
      printf("\n");
   }
   return 0;
}]

Output:

[Enter the number of rows: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15]

7. Conclusion


In this article, We've seen the most asked in the interview for freshers and fresh graduates.

C Program to Print Pyramids and Patterns

No comments:

Post a Comment

Please do not add any spam links in the comments section.