Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void triangle (int, int, int);
- int main ()
- {
- int h, d, s;
- printf("please enter the triangle's height:");
- if((!scanf("%d",&h))||h<2)
- {
- printf("Error. Height must be at least 2 \n");
- return 0;
- }
- printf("please enter number of $:");
- if(!(scanf("%d",&d))||d<1)
- {
- printf("Error. Number of $ must be at least 1\n");
- return 0;
- }
- printf("please enter number of spaces:");
- if(!(scanf("%d",&s))||s<1)
- {
- printf("Error. Number of spaces must be at least 1\n");
- return 0;
- }
- triangle(h, s, d);
- printf("height: %d \n", h);
- printf("number of $: %d \n", d);
- printf("number of spaces: %d \n", s);
- return 0;
- }
- void triangle (int h, int spaces, int dollars)
- {
- int i, j, space, cntD=0, cntC=0;
- for (i=0; i<h-1; i++)
- printf(" ");
- printf ("*\n");
- space=(h-2);
- for (i=2; i<h; i++)
- {
- for (j=0; j<space; j++)
- printf(" ");
- space--;
- printf("*");
- for (j=0; j<((i*2)-3); j++)
- {
- if (cntD<dollars)
- {
- printf("$");
- cntD++;
- continue;
- }
- if (cntC<spaces && cntD==dollars)
- {
- printf(" ");
- cntC++;
- }
- if(cntC==spaces)
- {
- cntC=0;
- cntD=0;
- }
- }
- printf("*\n");
- }
- for (i=0; i<((h*2)-1); i++)
- printf("*");
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement