Pattern Program in C: #Include
Pattern Program in C: #Include
Pattern Program in C: #Include
***
*****
*******
*********
We have shown five rows above; in the program, you will be asked to enter the numbers of
rows you want to print in the pyramid of stars.
Pattern program in C
1. #include <stdio.h>
2.
3. int main()
4. {
5. int row, c, n, s;
6.
7. printf("Enter the number of rows in pyramid of stars you wish to
see\n");
8. scanf("%d", &n);
9.
10. s = n;
11.
12. for (row = 1; row <= n; row++) // Loop to print rows
13. {
14. for (c = 1; c < s; c++) // Loop to print spaces in a row
15. printf(" ");
16.
17. s--;
18.
19. for (c = 1; c <= 2*row - 1; c++) // Loop to print stars in
a row
20. printf("*");
21.
22. printf("\n");
23. }
24.
25. return 0;
26. }
C pattern programs
Pattern:
*
*A*
*A*A*
*A*A*A*
1. #include<stdio.h>
2.
3. int main()
4. {
5. int n, c, k, space, count = 1;
6.
7. printf("Enter number of rows\n");
8. scanf("%d", &n);
9.
10. space = n;
11.
12. for (c = 1; c <= n; c++)
13. {
14. for (k = 1; k < space; k++)
15. printf(" ");
16.
17. for (k = 1; k <= c; k++)
18. {
19. printf("*");
20.
21. if (c > 1 && count < c)
22. {
23. printf("A");
24. count++;
25. }
26. }
27.
28. printf("\n");
29. space--;
30. count = 1;
31. }
32. return 0;
33. }
Pattern:
1
232
34543
4567654
567898765
C program:
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, c, row, num = 1, space;
6.
7. scanf("%d", &n);
8.
9. space = n - 1;
10.
11. for (row = 1; row <= n; row++)
12. {
13. num = row;
14.
15. for (c = 1; c <= space; c++)
16. printf(" ");
17.
18. space--;
19.
20. for (c = 1; c <= row; c++)
21. {
22. printf("%d", num);
23. num++;
24. }
25.
26. num = num - 2;
27.
28. for (c = 1 ; c < row; c++)
29. {
30. printf("%d", num);
31. num--;
32. }
33.
34. printf("\n");
35. }
36.
37. return 0;
38. }
Example 2: Program to print half pyramid a using
numbers
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Source Code
#include <stdio.h>
int main()
{
int i, j, rows;
C C C
D D D D
E E E E E
Source Code
#include <stdio.h>
int main()
{
int i, j;
char input, alphabet = 'A';
printf("Enter the uppercase character you want to print in last row: ");
scanf("%c",&input);
printf("\n");
}
return 0;
}
* * * *
* * *
* *
Source Code
#include <stdio.h>
int main()
{
int i, j, rows;
return 0;
}
1 2 3
1 2
Source Code
#include <stdio.h>
int main()
{
int i, j, rows;
return 0;
}
* * * * *
* * * * * * *
* * * * * * * * *
Source Code
#include <stdio.h>
int main()
{
int i, space, rows, k=0;
while(k != 2*i-1)
{
printf("* ");
++k;
}
printf("\n");
}
return 0;
}
Example 7: Program to print pyramid using numbers
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
Source Code
#include <stdio.h>
int main()
{
int i, space, rows, k=0, count = 0, count1 = 0;
while(k != 2*i-1)
{
if (count <= rows-1)
{
printf("%d ", i+k);
++count;
}
else
{
++count1;
printf("%d ", (i+k-2*count1));
}
++k;
}
count1 = count = k = 0;
printf("\n");
}
return 0;
}
* * * * * * * * *
* * * * * * *
* * * * *
* * *
Source Code
#include<stdio.h>
int main()
{
int rows, i, j, space;
printf("\n");
}
return 0;
}
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Source Code
#include <stdio.h>
int main()
{
int rows, coef = 1, space, i, j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
2 3
4 5 6
7 8 9 10
Source Code
#include <stdio.h>
int main()
{
int rows, i, j, number= 1;
printf("\n");
}
return 0;
}
Diamond pattern in C language: This code prints diamond pattern of stars. The diamond
shape is as follows:
*
***
*****
***
*
C programming code
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, c, k, space = 1;
6.
7. printf("Enter number of rows\n");
8. scanf("%d", &n);
9.
10. space = n - 1;
11.
12. for (k = 1; k <= n; k++)
13. {
14. for (c = 1; c <= space; c++)
15. printf(" ");
16.
17. space--;
18.
19. for (c = 1; c <= 2*k-1; c++)
20. printf("*");
21.
22. printf("\n");
23. }
24.
25. space = 1;
26.
27. for (k = 1; k <= n - 1; k++)
28. {
29. for (c = 1; c <= space; c++)
30. printf(" ");
31.
32. space++;
33.
34. for (c = 1 ; c <= 2*(n-k)-1; c++)
35. printf("*");
36.
37. printf("\n");
38. }
39.
40. return 0;
41. }
return 0;
}
*******************************************************
Statement - Copy element of one array into another
*******************************************************/
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
int arr1[30], arr2[30], i, num;
clrscr();
getch();
}