PATTERN PROGRAM IN C
C Program to Print Diamond Pattern
*
* C Program to Print Diamond Pattern using For Loop
*/
#include <stdio.h>
int main()
{
int number, i, k, count = 1;
printf("Enter number of rows: \n");
scanf("%d", &number);
count = number - 1;
for (k = 1; k <= number; k++)
{
for (i = 1; i <= count; i++)
printf(" ");
count--;
for (i = 1; i <= 2 * k - 1; i++)
printf("*");
printf("\n");
}
count = 1;
for (k = 1; k <= number - 1; k++)
{
for (i = 1; i <= count; i++)
printf(" ");
count++;
for (i = 1 ; i <= 2 *(number - k)- 1; i++)
printf("*");
printf("\n");
}
return 0;
}
Run Time Testcases
Testcase 1: In this case, we enter the number of rows as “5” to print the diamond pattern.
Enter number of rows: 5
*
***
*****
*******
*********
*******
*****
***
*
Testcase 2: In this case, we enter the number of rows as “3” to print the diamond pattern.
Enter number of rows: 3
*
***
*****
***
*
C Program to Print Floyd’s Triangle
/*
* C Program to Display Floyd’s triangle using while loop
*/
#include <stdio.h>
int main()
{
int i = 1, j = 0, rows = 7;
int num = 1;
while (i <= rows)
{
while (j <= i - 1)
{
printf("%d ", num);
j++;
num++;
}
j = 0;
i++;
printf("\n");
}
return 0;
}
Runtime Test Cases
In this case, the entered value is “7” to print the floyds triangle.
Enter the number of rows: 7
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
Pascal Triangle Program in C
/*
* C Program to Generate Pascal Triangle 1 D Array
*/
#include <stdio.h>
void main()
{
int array[30], temp[30], i, j, k, l, num; //using 2 arrays
printf("Enter the number of lines to be printed: ");
scanf("%d", &num);
temp[0] = 1;
array[0] = 1;
for (j = 0; j < num; j++)
printf(" ");
printf(" 1\n");
for (i = 1; i < num; i++)
{
for (j = 0; j < i; j++)
printf(" ");
for (k = 1; k < num; k++)
{
array[k] = temp[k - 1] + temp[k];
}
array[i] = 1;
for (l = 0; l <= i; l++)
{
printf("%3d", array[l]);
temp[l] = array[l];
}
printf("\n");
}
}
Program Output
Enter the number of lines to be printed: 4
1
1 1
1 2 1
1 3 3 1
Star Pattern Programs in C
/*
* C Program to Print Stars using Function
*/
#include <stdio.h>
void printN(char ch, int n)
{
int i;
for (i = 1; i <= n; i++)
{
printf("%c", ch);
}
}
int main(void)
{
int i, n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printN('*', i);
printf("\n");
}
return 0;
}
Run Time Testcases
In this case, we enter the number of rows as “7” to print the star pattern.
./star-pattern
Enter the number of rows: 7
*
**
***
****
*****
******
*******
Pyramid Patterns in C
/*
* C Program to Print Inverted Pyramid
*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
int rows;
int column;
int spaces;
printf("Enter the number of rows : ");
scanf(" %d",&rows);
printf("****--Inverted Pyramid---****\n\n");
for(int i=0; i<rows; i++)
{
for(int spaces=0;spaces<i; spaces++)
{
printf(" ");
}
for(int j=0;j<rows-i;j++)
{
printf("* ");
}
printf("\n");
}
printf("\n");
return 0;
}
Run Time Testcases
In this case, we enter the number of rows as “6” to print the inverted pyramid pattern.
Enter the number of rows: 6
****--Inverted Pyramid---****
* * * * * *
* * * * *
* * * *
* * *
* *
*
/*
* C Program to Print Inverted Half Pyramid
*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
int rows;
int column;
int spaces;
printf("Enter the number of rows: ");
scanf("%d",&rows);
printf("****--Half Inverted Pyramid---****\n\n");
for(int i=0; i<rows; i++)
{
for(int spaces =0;spaces<i;spaces++)
{
printf(" ");
}
for(int j=0;j<rows-i;j++)
{
printf("* ");
}
printf("\n");
}
printf("\n");
return 0;
}
Run Time Testcases
In this case, we enter the number of rows as “9” to print the inveted half pyramid pattern.
Enter the number of rows: 9
****--Half Inverted Pyramid---****
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
/*
* C Program to Print Left Aligned Pyramid
*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
int rows;
int column;
int spaces;
printf("Enter the number of rows : ");
scanf(" %d",&rows);
printf("\n");
//left pyramid....
printf("****--Left Pyramid---****\n");
for(int i=0; i<rows; i++)
{
for(int l=0;l<rows-1-i;l++)
{
printf(" ");
}
for(int j=0; j<=i;j++)
{
printf("* ");
}
printf("\n");
}
printf("\n");
return 0;
}
Run Time Testcases
In this case, we enter the number of rows as “7” to print the left aligned pyramid pattern.
Enter the number of rows: 7
****--Left Pyramid---****
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *