Pattern in C
Pattern in C
* * | 1 2 | A B
* * * | 1 2 3 | A B C
* * * * | 1 2 3 4 | A B C D
* * * * * | 1 2 3 4 5 | A B C D E
Q1. // C program to print right half pyramid pattern of star
#include <stdio.h>
int main()
int rows = 5;
printf("* ");
printf("\n");
return 0;
#include <stdio.h>
int main()
int rows = 5;
{
// second loop for printing number in each rows
printf("\n");
return 0;
#include <stdio.h>
int main()
int rows = 5;
printf("\n");
return 0;
}
* | 1 | A
* * | 1 2 | A B
* * * | 1 2 3 | A B C
* * * * | 1 2 3 4 | A B C D
* * * * * | 1 2 3 4 5 | A B C D E
Q4. c program to print left half pyramid pattern of star
#include <stdio.h>
int main()
int rows = 5;
printf(" ");
printf("* ");
printf("\n");
return 0;
#include <stdio.h>
int main()
{
int rows = 5;
printf(" ");
printf("\n");
return 0;
#include <stdio.h>
int main()
int rows = 5;
{
printf(" ");
printf("\n");
return 0;
* * * * * | 1 2 3 4 5 | A B C D E
* * * * | 1 2 3 4 | A B C D
* * * | 1 2 3 | A B C
* * | 1 2 | A B
* | 1 | A
#include <stdio.h>
int main()
{
int rows = 5;
// first loop to print all rows
for (int i = 0; i < rows; i++)
{
// first inner loop to print the * in each row
for (int j = 0; j < rows - i; j++)
{
printf("* ");
}
printf("\n");
}
}
Q8. // C program to print the inverted right half pyramid of numbers
#include <stdio.h>
int main()
{
int rows = 5;
* | 1 | A
* * * | 1 2 3 | A B C
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F
G
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G
H I
Q10. C program to print the full pyramid pattern of stars
#include <stdio.h>
int main()
{
int rows = 5;
// first loop to print all rows
for (int i = 0; i < rows; i++)
{
// inner loop 1 to print white spaces
for (int j = 0; j < 2 * (rows - i) - 1; j++)
{
printf(" ");
}
// inner loop 2 to print star * character
for (int k = 0; k < 2 * i + 1; k++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
Q11. // C program to print the full pyramid pattern of alphabets
#include <stdio.h>
int main()
{
int rows = 5;
// first loop to print all rows
for (int i = 0; i < rows; i++)
{
// inner loop 1 to print white spaces
for (int j = 0; j < 2 * (rows - i) - 1; j++)
{
printf(" ");
}
* * * * * | 1 2 3 4 5 | A B C D E
* * * * | 1 2 3 4 | A B C D
* * * | 1 2 3 | A B C
* * | 1 2 | A B
* | 1 | A
Q13. C program to print the inverted left half pyramid pattern of stars
#include <stdio.h>
int main()
{
int rows = 5;
// first loop for printing all rows
for (int i = 0; i < rows; i++)
{
return 0;
}
Q14. C program to print the inverted left half pyramid pattern of numbers
#include <stdio.h>
int main()
{
int rows = 5;
// first loop for printing all rows
for (int i = 0; i < rows; i++)
{
return 0;
}
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H
I
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * | 1 2 3 4 5 | A B C D E
* * * | 1 2 3 | A B C
* | 1 | A
Q16. C program to print the inverted full pyramid pattern of stars
#include <stdio.h>
int main()
{
int rows = 5;
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * | 1 2 3 4 5 | A B C D E
Q19. C Program to print the rhombus pattern using * star
#include <stdio.h>
int main()
{
int rows = 5;
* | 1 | A
* * * | 1 2 3 | A B C
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H
I
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * | 1 2 3 4 5 | A B C D E
* * * | 1 2 3 | A B C
* | 1 | A
Q22. C Program to print diamond pattern using star *
#include <stdio.h>
int main()
{
int n = 5;
// first outer loop to iterate through each row
for (int i = 0; i < 2 * n - 1; i++)
{
int main()
{
int rows = 5;
// outer loop to iterator through each row
for (int i = 0; i < rows; i++)
{
// inner loop to print * star in each row
for (int j = 0; j < rows; j++) {
// statement to check boundry condition
if (i > 0 && i < rows - 1 && j > 0 && j < rows - 1)
{
printf(" ");
}
else
{
printf("* ");
}
}
printf("\n");
}
return 0;
}
Q29. C Program to print hollow square pattern using numbers
#include <stdio.h>
int main()
{
int rows = 5;
// outer loop to iterator through each row
for (int i = 0; i < rows; i++)
{
// inner loop to print number in each row
for (int j = 0; j < rows; j++)
{
// statement to check boundry condition
if (i > 0 && i < rows - 1 && j > 0 && j < rows - 1)
{
printf(" ");
}
else
{
printf("%d ", j + 1);
}
}
printf("\n");
}
return 0;
}
Q30. C Program to print hollow square pattern using alphabet
#include <stdio.h>
int main()
{
int rows = 5;
// outer loop to iterator through each row
for (int i = 0; i < rows; i++)
{
// inner loop to print alphabet in each row
for (int j = 0; j < rows; j++)
{
// statement to check boundry condition
if (i > 0 && i < rows - 1 && j > 0 && j < rows - 1)
{
printf(" ");
}
else
{
printf("%c ", j + 'A');
}
}
printf("\n");
}
return 0;
}
* | 1 | A
* * | 1 3 | A C
* * | 1 5 | A E
* * | 1 7 | A G
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H
I
Q31. C program to print hollow full pyramid using star *
#include <stdio.h>
int main()
{
int rows = 5;
// first outer loop to iterate through each loop
for (int i = 0; i < rows; i++)
{
// first inner loop to print leading whitespaces
for (int j = 0; j < 2 * (rows - i) - 1; j++)
{
printf(" ");
}
// second inner loop to print stars * and inner whitespaces
for (int k = 0; k < 2 * i + 1; k++) {
if (k == 0 || k == 2 * i || i == rows - 1)
{
printf("* ");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
Q32. C program to print hollow full pyramid using numbers
#include <stdio.h>
int main()
{
int rows = 5;
// first outer loop to iterate through each loop
for (int i = 0; i < rows; i++)
{
// first inner loop to print leading whitespaces
for (int j = 0; j < 2 * (rows - i) - 1; j++)
{
printf(" ");
}
// second inner loop to print numbers and inner whitespaces
for (int k = 0; k < 2 * i + 1; k++)
{
if (k == 0 || k == 2 * i || i == rows - 1)
{
printf("%d ", k + 1);
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
Q33. C program to print hollow full pyramid using alphabets
#include <stdio.h>
int main()
{
int rows = 5;
// first outer loop to iterate through each loop
for (int i = 0; i < rows; i++)
{
// first inner loop to print leading whitespaces
for (int j = 0; j < 2 * (rows - i) - 1; j++)
{
printf(" ");
}
// second inner loop to print alphabets and inner whitespaces
for (int k = 0; k < 2 * i + 1; k++)
{
if (k == 0 || k == 2 * i || i == rows - 1)
{
printf("%c ", k + 'A');
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H
I
* * | 1 7 | A G
* * | 1 5 | A E
* * | 1 3 | A C
* | 1 | A
Q34. C Program to print hollow full pyramid pattern using star
#include <stdio.h>
int main()
{
int rows = 5;
// first loop iterating through each row
for (int i = 0; i < rows; i++)
{
// first inner loop to print leading white space
for (int j = 0; j < 2 * i + 1; j++)
{
printf(" ");
}
// second inner loop to print star* and hollow white space
for (int k = 0; k < 2 * (rows - i) - 1; k++)
{
if (k == 0 || k == 2 * (rows - i) - 2 || i == 0)
printf("* ");
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
Q35. C Program to print hollow full pyramid pattern using numbers
#include <stdio.h>
int main()
{
int rows = 5;
// first loop iterating through each row
for (int i = 0; i < rows; i++)
{
// first inner loop to print leading white space
for (int j = 0; j < 2 * i + 1; j++)
{
printf(" ");
}
// second inner loop to print number and hollow white space
for (int k = 0; k < 2 * (rows - i) - 1; k++)
{
if (k == 0 || k == 2 * (rows - i) - 2 || i == 0)
printf("%d ", k + 1);
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
Q36. C Program to print hollow full pyramid pattern using alphabets
#include <stdio.h>
int main()
{
int rows = 5;
// first loop iterating through each row
for (int i = 0; i < rows; i++)
{
// first inner loop to print leading white space
for (int j = 0; j < 2 * i + 1; j++)
{
printf(" ");
}
* | 1 | A
* * | 1 3 | A C
* * | 1 5 | A E
* * | 1 7 | A G
* * | 1 9 | A
I
* * | 1 7 | A G
* * | 1 5 | A E
* * | 1 3 | A C
* | 1 | A
Q37. C Program to print the hollow diamond pattern using star
#include <stdio.h>
int main()
{
int n = 5;
// first outer loop to iterator through each row
for (int i = 0; i < 2 * n - 1; i++)
{
// assigning values to the comparator according to the row number
int comp;
if (i < n) {
comp = 2 * (n - i) - 1;
}
else
{
comp = 2 * (i - n + 1) + 1;
}
// first inner loop to print leading whitespaces
for (int j = 0; j < comp; j++)
{
printf(" ");
}
// second inner loop to print star * and inner whitespaces
for (int k = 0; k < 2 * n - comp; k++)
{
if (k == 0 || k == 2 * n - comp - 1)
{
printf("* ");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
Q38. C Program to print the hollow diamond pattern using numbers
#include <stdio.h>
int main()
{
int n = 5;
// first outer loop to iterator through each row
for (int i = 0; i < 2 * n - 1; i++)
{
// assigning values to the comparator according to the row number
int comp;
if (i < n)
{
comp = 2 * (n - i) - 1;
}
else
{
comp = 2 * (i - n + 1) + 1;
}
// first inner loop to print leading whitespaces
for (int j = 0; j < comp; j++) {
printf(" ");
}
Pascal’s Triangle in C
A Pascal’s Triangle is a triangular array of binomial coefficients where the
nth row contains the binomial coefficients nC0, nC1, nC2, ……. nCn. The
following example demonstrates one of the methods using which we can
print Pascal’s Triangle Pattern.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Q45.C program to print the pascal's triangle pattern
#include <stdio.h>
int main()
{
int rows = 5;
// outer loop for rows
for (int i = 1; i <= rows; i++)
{
// inner loop 1 for leading white spaces
for (int j = 0; j < rows - i; j++)
{
printf(" ");
}
int C = 1; // coefficient
// inner loop 2 for printing numbers
for (int k = 1; k <= i; k++)
{
printf("%d ", C);
C = C * (i - k) / k;
}
printf("\n");
}
return 0;
}