Patterns in C
Patterns in C : This article is a complete source code library for every pattern program in C like number pattern programs, star (*) pattern programs, triangle or pyramid pattern programs, alphabet pattern programs.
-
Number Pattern Programs
- Pascal's Triangle
- Floyd's Triangle
- Inverted Triangle Star * Pattern Programs
- Diamond
- Half Pyramid
- Inverted Pyramid Alphabets Pattern Programs
- Half Triangle Using Alphabet
- Inverted Triangle Using Alphabet
Diamond Pattern in C
*
***
*****
*******
*********
*******
*****
***
*
Here is the source code of c program to print or the display diamond pattern using stars.
#include<stdio.h>
int main()
{
int i,n,j,s=1;
printf("\n Enter number of rows:- ");
scanf("%d",&n);
printf("\n This is the diamond pattern in stars: \n");
s=n-1;
for(j=1;j<=n;j++)
{
for(i=1;i<=s;i++)
printf(" ");
s--;
for(i=1;i<=2*j-1;i++)
printf("*");
printf("\n");
}
s=1;
for(j=1;j<=n-1;j++)
{
for(i=1;i<=s;i++)
printf(" ");
s++;
for(i=1;i<=2*(n-j)-1;i++)
printf("*");
printf("\n");
}
return 0;
}
/* Output of above code:-
Enter number of rows:- 5
This is the diamond pattern in stars:
*
***
*****
*******
*********
*******
*****
***
*
*/
If you want the algorithm or general idea behind how to print diamond pattern in C visit -
C program to print diamond pattern
Print Pascal's Triangle in C
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Here is the source code to print Pascal's triangle in C.
#include<stdio.h>
int main()
{
int i, j, coeff = 1, space;
printf("\n Enter 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)
coeff = 1;
else
coeff = coeff*(i-j+1)/j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
/* Output of above code:-
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
*/
Program to Print Floyd's Triangle in C
1
2 3
4 5 6
7 8 9 10
Here is the source code of C program to print half triangle of numbers.
#include<stdio.h>
int main()
{
int i,j,c=0;
printf("\n");
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
printf(" %d",c++);
printf("\n \n");
}
return 0;
}
/* Output of above code:-
1
2 3
4 5 6
7 8 9 10
*/
Program to Print half Triangle (Pyramid) Pattern using Stars (*) in C
*
* *
* * *
Here is the source code of C program to print half triangle of stars (*).
#include<stdio.h>
int main()
{
int i,j,c=0;
printf("\n");
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
printf(" %d",c++);
printf("\n \n");
}
return 0;
}
/* Output of above code:-
*
* *
* * *
*/
Print Star Triangle or Pyramid in C
*
***
*****
*******
*********
Here is the source code of conduct program to print full triangle or pyramid using stars (* 🌟).
#include<stdio.h>
int main()
{
int row, cols, size, sp;
printf("\n Enter number of rows to print star triangle:- ");
scanf("%d", &size);
sp = size;
for(row = 1; row <= size; row++)
{
for (cols = 1; cols < sp; cols++)
printf(" ");
sp--;
for (cols = 1; cols <= 2*row - 1; cols++)
printf("*");
printf("\n");
}
return 0;
}
Program to Print half Triangle (Pyramid) Pattern Using Alphabets in C
A
B C
D E F
Here is the source code of C program to print half triangle of alphabets.
#include<stdio.h>
int main()
{
int c,a,e,l,num,m;
e=64;
printf("\n");
for (l=1;l<=3;l++)
{
for (num=e+1;num<=e+l;num++)
printf("%c\t",num);
printf("\n");
e=e+l;
}
printf("\n");
}
/* Output of above code:-
A
B C
D E F
*/
Program to Print Inverted half Triangle (Pyramid) Number Pattern in C
1 2 3 4
1 2 3
1 2
1
Here is the source code C program to print inverted half triangle using *.
#include<stdio.h>
int main()
{
int i,j;
printf("\n");
for (i=4;i>=1;i--)
{
for (j=1;j<=i;j++)
printf(" %d\t",j);
printf("\n");
}
printf("\n");
return 0;
}
/* Output of above code:-
1 2 3 4
1 2 3
1 2
1
*/
Program to Print Inverted half Triangle (Pyramid) Pattern using Stars (*) in C
* * * *
* * *
* *
*
Here is the source code C program to print inverted half triangle using *.
#include<stdio.h>
int main()
{
int i,j;
printf("\n");
for (i=4;i>=1;i--)
{
for (j=1;j<=i;j++)
printf("*\t");
printf("\n");
}
printf("\n");
return 0;
}
/* Output of above code:-
* * * *
* * *
* *
*
*/
Program to Print Inverted half Triangle (Pyramid) using Alphabets in C
A B C D
A B C
A B
A
Here is the source code of Conduct program to print inverted half trainlge using alphabets.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c,a,e,n,l,num;
printf("\n How many lines:- ");
scanf("%d",&n);
printf("\n");
if ((n>26) || (n<=0))
{
printf("\n Error:You can specify only 26 l asines. please specify a numeric value \n \n");
exit(1);
}
for (l=n;l>=1;l--)
{
e=64+l;
for (num=65;num<=e;num++)
printf("%c\t",num);
printf("\n");
}
printf("\n");L;
return 0;
}
/* Output of above code:
How many lines:- 4
A B C D
A B C
A B
A
*/
Putting too much pattern programs in one post will make it heavy to load so take a lot at other pattern programs that are implemented in C :
Infix to Postfix conversion in C is done by taking the priority of operators in consideration. We will consider the priority of operators as follows:
Let us do this step by step,
A+B*C
A+BC* - B, C operands are combined and * is written on right side. Now A and BC* are two operands.
ABC*+ - Now combine A and BC* and write + on right hand side
ABC*+ is the postfix expression of given infix expression.
- Brackets
- Exponential (^ or $)
- Arithmetic (*,/,%)
- Arithmetic (+, -)
Infix to Postfix Conversion in C
For example, let the infix expression be A+B*C. In this infix expression * will be converted first and + will be converted last, this is usual thing.Let us do this step by step,
A+B*C
A+BC* - B, C operands are combined and * is written on right side. Now A and BC* are two operands.
ABC*+ - Now combine A and BC* and write + on right hand side
ABC*+ is the postfix expression of given infix expression.
Infix to Postfix Program in C
Here is the source code of C program to convert infix to postfix using stack.#include<studio.h>
#define SIZE 100
typedef strict
{
int data[SIZE];
int top;
}STACK;
void initStack(STACK *pt)
{
pt->top = -1;
}
int isEmpty(STACK *pt)
{
return(pt->top==-1);
}
int isFull(STACK *pt)
{
return(pt->top==SIZE-1);
}
int pop(STACK *pt)
{
return(pt->data[pt->top--]);
}
void push(STACK *pt, int n)
{
pt->data[++pt->top] = n
}
int stackTop(STACK *pt)
{
return(pt->data[pt->top]);
}
int priority(char ch)
{
switch(ch)
{
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
}
return 0;
}
void infixToPostfix(char infix[100], char postfix[100])
{
char ch, ch2;
int i, j = 0;
STACK s;
initStack(&s);
for(i=0;infix[i]!='\0';i++)
{
ch = infix[i];
switch (ch)
{
case '(': push(&s, ch);
break;
case '+':
case '-':
case '*':
case '/':
case '%':
if(isEmpty(&s)
push(&s,ch);
else
{
while(priority (stackTop(&s)) >= priority (ch))
postfix[j++] = pop(&s);
push(&s, ch);
}
break;
case ')':
while(ch1 = pop(&s)!='(')
postfix [j++] = ch1;
break;
default: postfix[j++] = ch;
}
}
while(! isEmpty(&s))
postfix[j++] = pop(&s);
postfix[j] = '\0';
}
int main()
{
char infix[100], postfix [100];
printf("\n Enter the infix expression:- ");
scanf(" %s",infix);
convert(infix, postfix);
printf("\n The Postfix String is %s ", postfix);
return 0;
}
/* Output of above code:-
Enter the infix expression:- A+B*C
The Postfix String is ABC*+
*/