C Language Programs
21. Pattern 1
•
• •
• • •
• • • •
• • • • •
22. Pattern 2
• • • • •
• • • •
• • •
• •
•
23. Pattern 3
•
• •
• • •
• • • •
• • • • •
24. Pattern 4
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
25. Pattern 5
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
26. Pattern 6
A
B C
D E F
G H I J
K L M N O
27. Pattern 7
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
28. Pattern 8 (Binary Pattern)
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
29. Pattern 9
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
30. Pattern 10
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
31. Pattern 11
1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 4 3 2 1
32. Floyd's triangle
1
2 3
4 5 6
7 8 9 10
11 12 13 14
33. Pyramid
*
* *
* * *
* * * *
* * * * *
34. Pyramid 2
*
*A*
*A*A*
*A*A*A*
*A*A*A*A*
35. Number Pyramid
1
232
34543
4567654
567898765
•
• •
• • •
• • • •
• • • • •
22. Pattern 2
• • • • •
• • • •
• • •
• •
•
23. Pattern 3
•
• •
• • •
• • • •
• • • • •
24. Pattern 4
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
25. Pattern 5
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
26. Pattern 6
A
B C
D E F
G H I J
K L M N O
27. Pattern 7
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
28. Pattern 8 (Binary Pattern)
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
29. Pattern 9
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
30. Pattern 10
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
31. Pattern 11
1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 4 3 2 1
32. Floyd's triangle
1
2 3
4 5 6
7 8 9 10
11 12 13 14
33. Pyramid
*
* *
* * *
* * * *
* * * * *
34. Pyramid 2
*
*A*
*A*A*
*A*A*A*
*A*A*A*A*
35. Number Pyramid
1
232
34543
4567654
567898765
36. Pascal triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
37. Pascal triangle without using function
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
38. Pascal triangle 2
1
121
12321
1234321
123454321
39. Number Alphabet Pattern
1
A B
2 3 4
C D E F
5 6 7 8 9
G H I J K L
40. Number Diamond Pattern
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7
1 2 3 4 5
1 2 3
1
code
21. Pattern 1
•
• •
• • •
• • • •
• • • • •
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
clrscr();
printf("Enter number : ");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
{
printf("• ");
}
printf("\n");
}
getch();
}
22. Pattern 2
• • • • •
• • • •
• • •
• •
•
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
clrscr();
printf("Enter number : ");
scanf("%d", &n);
for(i=n; i>=1; i--)
{
for(j=1;j<=i;j++)
{
printf("• ");
}
printf("\n");
}
getch();
}
23. Pattern 3
•
• •
• • •
• • • •
• • • • •
#include<stdio.h>
#include<conio.h>
void main()
{
char ch = '*';
int n;
int i, j, no_of_spaces = 0, spaceCount;
printf("Enter number : ");
scanf("%d", &n);
printf("\n");
no_of_spaces = n - 1;
for (i = 1; i <= n; i++)
{
for (spaceCount = no_of_spaces; spaceCount >= 1; spaceCount--)
{
printf(" ");
}
for (j = 1; j <= i; j++)
{
printf("%2c", ch);
}
printf("\n");
no_of_spaces--;
}
getch();
}
24. Pattern 4
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j;
printf("Enter number : ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d ", j);
}
printf("\n");
}
getch();
}
25. Pattern 5
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
#include <stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
printf("Enter number : ");
scanf("%d", &n);
printf("\n");
for (i = 1; i <= n; i++)
{
for (j = i; j >= 1; j--)
{
printf("%d ", j);
}
printf("\n");
}
getch();
}
26. Pattern 6
A
B C
D E F
G H I J
K L M N O
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
clrscr();
printf("Enter number : ");
scanf("%d", &n);
int c = 'A';
for (i = 0; i < n; i++)
{
for (j = 0; j <= i; j++)
{
printf("%c", c);
c = c + 1;
}
printf("\n");
}
getch();
}
27. Pattern 7
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
clrscr();
printf("Enter number : ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
printf("%d", j);
for (j = i - 1; j >= 1; j--)
printf("%d", j);
printf("\n");
}
getch();
}
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
clrscr();
printf("Enter number : ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
printf("%d", j);
for (j = i - 1; j >= 1; j--)
printf("%d", j);
printf("\n");
}
getch();
}
28. Pattern 8 (Binary Pattern)
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
int count = 1;
clrscr();
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d", count++ % 2);
if (j == i && i != 5)
printf("\n");
}
if (i % 2 == 0)
count = 1;
else
count = 0;
}
getch();
}
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
int count = 1;
clrscr();
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d", count++ % 2);
if (j == i && i != 5)
printf("\n");
}
if (i % 2 == 0)
count = 1;
else
count = 0;
}
getch();
}
29. Pattern 9
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k, l, n;
clrscr();
printf("Enter number : ");
scanf("%d", &n);
printf("\n");
for (i = n; i >= 1; i--)
{
for (j = 1; j <= i; j++)
printf("%d", j);
printf("\n");
}
getch();
}
30. Pattern 10
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
clrscr();
for (i = 1; i <= 5; i++)
{
for (j = 5; j >= 1; j--)
{
if (j <= i)
{
printf("%d", j);
}
else
{
printf(" ");
}
}
printf("\n");
}
getch();
}
31. Pattern 11
1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 4 3 2 1
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
clrscr();
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= 5; j++)
{
if (j <= i)
{
printf("%d ", j);
}
else
{
printf(" ");
}
}
for (j = 5; j >= 1; j--)
{
if (j <= i)
{
printf("%d ", j);
}
else
{
printf(" ");
}
}
printf("\n");
}
getch();
}
32. Floyd's triangle
1
2 3
4 5 6
7 8 9 10
11 12 13 14
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, c, a = 1;
clrscr();
printf("Enter the number of rows : ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (c = 1; c <= i; c++) {
printf("%d ", a);
a++;
}
printf("\n");
}
getch();
}
33. Pyramid
*
* *
* * *
* * * *
* * * * *
#include<stdio.h>
#include<conio.h>
void main()
{
int row, c, n, temp;
clrscr();
printf("Enter the number of rows : ");
scanf("%d", &n);
temp = n;
for (row = 1; row <= n; row++)
{
for (c = 1; c < temp; c++)
printf(" ");
temp--;
for (c = 1; c <= 2 * row - 1; c++)
printf("*");
printf("\n");
}
getch();
}
34. Pyramid 2
*
*A*
*A*A*
*A*A*A*
*A*A*A*A*
#include<stdio.h>
#include<conio.h>
void main()
{
int n, c, k, space, count = 1;
clrscr();
printf("Enter the number of rows : ");
scanf("%d", &n);
space = n;
for (c = 1; c <= n; c++)
{
for (k = 1; k < space; k++)
printf(" ");
for (k = 1; k <= c; k++)
{
printf("*");
if (c > 1 && count < c)
{
printf("A");
count++;
}
}
printf("\n");
space--;
count = 1;
}
getch();
}
35. Number Pyramid
1
232
34543
4567654
567898765
#include<stdio.h>
#include<conio.h>
void main()
{
int n, c, d, num = 1, space;
clrscr();
printf("Enter the number of rows : ");
scanf("%d", &n);
space = n - 1;
for (d = 1; d <= n; d++)
{
num = d;
for (c = 1; c <= space; c++)
printf(" ");
space--;
for (c = 1; c <= d; c++)
{
printf("%d", num);
num++;
}
num--;
num--;
for (c = 1; c < d; c++)
{
printf("%d", num);
num--;
}
printf("\n");
}
getch();
}
36. Pascal triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
#include<stdio.h>
#include<conio.h>
long fact(int);
void main()
{
int line, i, j;
clrscr();
printf("Enter the number : ");
scanf("%d", &line);
for (i = 0; i < line; i++)
{
for (j = 0; j < line - i - 1; j++)
{
printf(" ");
}
for (j = 0; j <= i; j++)
{
printf("%ld ", fact(i) / (fact(j) * fact(i - j)));
}
printf("\n");
}
getch();
}
long fact(int num)
{
long f = 1;
int i = 1;
while (i <= num)
{
f = f * i;
i++;
}
return f;
}
37. Pascal triangle without using function
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y, n, a, z, s;
clrscr();
printf("Enter the number : ");
scanf("%d", &n);
s = n;
for (x = 0; x <= n; x++)
{
a = 1;
for (z = s; z >= 0; z--)
printf(" ");
s--;
for (y = 0; y <= x; y++)
{
printf("%d ", a);
a = (a * (x - y) / (y + 1));
}
printf("\n");
}
getch();
}
38. Pascal triangle 2
1
121
12321
1234321
123454321
#include<stdio.h>
#include<conio.h>
void main()
{
int n, c, k, number = 1, space = n;
clrscr();
printf("Enter number of rows : ");
scanf("%d", &n);
printf("\n");
space = n;
for (c = 1; c <= n; c++)
{
for (k = space; k > 1; k--)
printf(" ");
space--;
for (k = 1; k <= 2 * c - 1; k++)
{
if (k <= c)
{
printf("%d", number);
if (k < c)
number++;
}
else
{
number--;
printf("%d", number);
}
}
number = 1;
printf("\n");
}
getch();
}
39. Number Alphabet Pattern
1
A B
2 3 4
C D E F
5 6 7 8 9
G H I J K L
#include<stdio.h>
#include<conio.h>
void main()
{
int num, r, c;
int i = 1;
char ch = 'A';
clrscr();
printf("Enter the number of rows : ");
scanf("%d", &num);
printf("\n");
for (r = 1; r <= num; r++)
{
for (c = 1; c <= r; c++)
{
if (r % 2 == 0)
{
printf(" %c", ch++);
}
else
{
printf(" %d", i++);
}
}
printf("\n");
}
getch();
}
40. Number Diamond Pattern
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7
1 2 3 4 5
1 2 3
1
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
clrscr();
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("%d",k);
}
printf("\n");
}
for(i=4;i>=1;i--)
{
for(j=5;j>i;j--)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("%d",k);
}
printf("\n");
}
getch();
}
THANK YOU
0 Comments