Logo 
Search:

C Programming Forum

Ask Question   UnAnswered
Home » Forum » C Programming       RSS Feeds

A triangle with $ spaces inside

  Asked By: Alex    Date: Jul 21    Category: C Programming    Views: 1157
  

Hello!

I am new in this forum, and hope to learn new things here. I am very glad to join!

I need to write a program that prints a triangle like this:

*
*$*
*$$$*
*$$ $*
*$$ $$$*
*$$$$ $$$*
*$ $$$$$ $*
* $$$$$ $$$$*
*****************
The picture isn't very accurate, but as you can see, this is a triangle with $ spaces inside.

The things is this: The program needs to print a $ instead of a space , and then from the 2nd line it counts 5$ and then prints a double space.
I tried to do this, but I only managed to do something like this:


*
*$*
*$$$*
*$$$$$*
*$$$$$$$*
*$$$$$$$$$*
*$$$$$$$$$$*

I know that I should write a condition within the loop, but I don't know what to write.

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Alex Lutsenko     Answered On: Jul 24

Ok, I managed to fix it.. here's the fixed code, I hope this will help others:

# include <stdio.h>
# include <stdlib.h>


int main()
{

int t,spaceNum=0,d=0,counter=0,i, j, k, n;

printf("Enter triangle height\n");
scanf("%d", &n);
if (n < 2) return 0;
printf("Enter the number of $\n");
scanf("%d", &d);
printf("Enter number of spaces\n");
scanf("%d", &spaceNum);
printf("\n");
for (i = 0; i< n ; i++)
printf(" ");

printf("*\n");
for (i = 1; i<n-1 ; i++)
{
for (j = n - i; j>0; j--)
printf(" ");
printf("*");
for (k = 0; k<2 * i-1; k++)

if (k >= 1 || k<(2*i-1))
{
counter = counter + 1;
if (counter == d+1)
{
for (t = 1; t <= spaceNum; t++)
{
printf(" ");
}
counter = 0;
}
else printf("$");
}
printf("*\n");
}
printf(" ");
for (i = 1; i<=2* n-1 ; i++)
printf("*");

printf("\n");
printf("\n");

printf("The height of triangle is %d\n", n);
printf("The number of $ is %d\n", d );
printf("The number of space is %d\n", spaceNum);

getchar();
getchar();
}


 
Didn't find what you were looking for? Find more on A triangle with $ spaces inside Or get search suggestion and latest updates.
This post is locked for further answers.




Tagged: