Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- number_pattern_pyramid.c
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <stdlib.h> // for abs()
- /*
- - - - *-
- - - *-*-*-
- - *-*-*-*-*-
- *-*-*-*-*-*-*-
- row ns nc n = 4
- 0 3 1
- 1 2 3
- 2 1 5
- 3 0 7
- ns = n-row-1;
- nc = 2*row+1;
- */
- void pyramid_of_char_v1(int n)
- {
- int i, row, ns, nc; // ns number of spaces, nc number of chars
- for (row=0; row<n; row++) // print rows one by one row
- {
- ns = n-row-1; // calculate ns number of spaces
- nc = 2*row+1; // calculate nc number of chars
- for (i=0; i<ns; i++) // print ns spaces " "
- printf("- ");
- for (i=0; i<nc; i++) // print nc "%d "
- printf("*-");
- printf("\n\n"); // print new row
- }
- }
- /*
- - - - 1-
- - - 2-1-2-
- - 3-2-1-2-3-
- 4-3-2-1-2-3-4-
- */
- void pyramid_of_numbers_v1(int n)
- {
- int i, row, ns, nc, num; // ns number of spaces, nc number of chars
- for (row=0; row<n; row++) // print rows one by one row
- {
- ns = n-row-1; // calculate ns number of spaces
- nc = 2*row+1; // calculate nc number of chars
- num = -(row+2); // calculate number
- for (i=0; i<ns; i++) // print ns spaces " "
- printf("- ");
- for (i=0; i<nc; i++) // print nc "%d "
- {
- num++;
- if(num==0)
- num=2;
- printf("%d-", abs(num) );
- }
- printf("\n\n"); // print new row
- }
- }
- /*
- ---*
- --***
- -*****
- *******
- row ns nc n = 4
- 0 3 1
- 1 2 3
- 2 1 5
- 3 0 7
- ns = n-row-1;
- nc = 2*row+1;
- */
- void pyramid_of_char_v2(int n)
- {
- int i, row, ns, nc; // ns number of spaces, nc number of chars
- for (row=0; row<n; row++) // print rows one by one row
- {
- ns = n-row-1; // calculate ns number of spaces
- nc = 2*row+1; // calculate nc number of chars
- for (i=0; i<ns; i++) // print ns spaces " "
- printf("-");
- for (i=0; i<nc; i++) // print nc "*"
- printf("*");
- printf("\n\n"); // print new row
- }
- }
- /*
- 1
- 212
- 32123
- 4321234
- */
- void pyramid_of_numbers_v2(int n)
- {
- int i, row, ns, nc, num; // ns number of spaces, nc number of chars
- for (row=0; row<n; row++) // print rows one by one row
- {
- ns = n-row-1; // calculate ns number of spaces
- nc = 2*row+1; // calculate nc number of chars
- num = -(row+2); // calculate number
- for (i=0; i<ns; i++) // print ns spaces " "
- printf("-");
- for (i=0; i<nc; i++) // print nc "%d"
- {
- num++;
- if(num==0)
- num=2;
- printf("%d", abs(num) );
- }
- printf("\n\n"); // print new row
- }
- }
- int main(void)
- {
- int n = 5; // number of rows
- printf("\n\t\t pyramid_of_char_v1() \n\n");
- pyramid_of_char_v1(n);
- printf("\n");
- printf("\n\t\t pyramid_of_numbers_v1() \n\n");
- pyramid_of_numbers_v1(n);
- printf("\n");
- printf("\n\t\t pyramid_of_char_v2() \n\n");
- pyramid_of_char_v2(n);
- printf("\n");
- printf("\n\t\t pyramid_of_numbers_v2() \n\n");
- pyramid_of_numbers_v2(n);
- printf("\n");
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement