# Triangle Pattern

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-11-2002
Unregistered
Triangle Pattern
I am trying to write a program that will look like a triangle. This is the pattern that I am trying to accomplish.

8\$\$\$\$\$\$\$
\$8\$\$\$\$\$\$
\$\$8\$\$\$\$\$
\$\$\$8\$\$\$\$
\$\$\$\$8\$\$\$
\$\$\$\$\$8\$\$
\$\$\$\$\$\$8\$
\$\$\$\$\$\$\$8

here is my code.#include <stdio.h>

int main (void)
{
int row;
int col;
int limit;

printf("\nPlease enter a number between 2 and 9: ");
scanf("%d", &limit);

for (row = 1; row<=limit; row++)
{
for (col = 1; col<=limit; col++)
if (row >= col)
printf("%d", col);
else
printf("\$");
printf("\n");
}
return 0;
}

I know that this isn't correct. can someone help me out
• 02-11-2002
Prelude
Something like this?
Code:

```#include <stdio.h> #include <stdlib.h> int main( void ) {   int i, j, k;   int spaceCount, maxSpace = 3;   for ( i = 1; i <= 8; i *= 2 ) {     spaceCount = maxSpace;     while ( spaceCount > 0 ) {       printf ( " " );       spaceCount--;     }     for ( j = 1; j <= i; j *= 2 ) {       printf ( "%d", j );       if ( j == i && j > 1 ) {         for ( k = j / 2; k >= 1; k /= 2 )           printf ( "%d", k );       }     }     printf ( "\n" );     maxSpace--;   }   return EXIT_SUCCESS; }```
-Prelude
• 02-11-2002
SavesTheDay
With a few minutes of work I put together a small bit of code which produces this effect....in the code I use a blank "space" character instead of the 8....but you can replace it with the 8....

Code:

```#include <stdio.h> int main (void) {         int row,limit,col=1,x;         printf("\n\tEnter a Number Between 2 and 9:  ");         scanf("%i", &limit);         printf("\n\t");         if ((limit<2) || (limit>9))         {                 return 0;         }                 while (col<=limit)         {                 for (x=0; x<limit; x++)                         {                                 printf("%c",36);                                                                 if ((col-1)==x)                                                 {                                                                        printf("\b%c",32);                                                 }                         }                 col++;                 printf("\n\t");         }         return 0; }```

%c, 36 Just Prints the \$ Character
%c, 32 Just Prints a Space Character

It keeps a counter of what line it's on....everytime the line number equals the character number, it goes \b to go back one space, and prints a space instead...just run it. enjoi.
• 02-11-2002
SavesTheDay
....to prelude......your code didn't do what he wanted...i don't think? he made a weird triangle shape up top..run my code...i made it print the funny triangles....yours is the ACTUAL triangle...his code was printing 2 sideways triangles..i wrote my code to the way i thought he wanted it..<shrugs> maybe i was wrong.
• 02-11-2002
Prelude
To be honest I didn't even read his post too carefully, usually when they say triangle program it means the one I posted. Teachers love that one. ;)

-Prelude
• 02-11-2002
SavesTheDay
mmmmmmm....who knows......i like the one i created......although it makes 2 triangles and they are tilted...i think that was the pattern he was going for....guess he will respond next time he reads.
• 02-11-2002
Prelude
Here's the code that prints out exactly what you described
Code:

```#include <stdio.h> #include <stdlib.h> int main ( void ) {   int i, j, max;   printf ( "Enter a number: " );   scanf ( "%d", &max );   for ( i = 0; i < max; ++i ) {     for ( j = 0; j < max; ++j ) {       if ( j == i )         printf ( "%d", max );       else         printf ( "\$" );     }     printf ( "\n" );   }   return EXIT_SUCCESS; }```
-Prelude
• 02-11-2002
Unregistered
Here's the triangle pattern
I am new to this language, so bear with me if its not the neatest.

int main (void)
{
int row;
int col;
int limit;

printf("\nPlease enter a number between 2 and 9: ");
scanf("%d", &limit);

for (row = 1; row<=limit; row++)
{
for (col = 1; col<=limit; col++)
if (row > col || row < col)
printf("\$");
else
printf("%d", limit);
printf("\n");
}
return 0;
}

I am truely amazed that I figured this one out. I am not very good at programming
• 02-11-2002
Prelude
>I am not very good at programming
It comes gradually, we were all there at one point, so no worries :)

BTW, if you want to neaten up your code by indenting it try using code tags. The syntax for them can be found here. Once indented your code doesn't look bad at all.

-Prelude
• 02-12-2002
SavesTheDay
mm...my program did that too...i thought he was asking for help on how to do it........not showing us that he did it...*shrugs*
• 02-12-2002
dmboasisu2
Just curious on how you would do the opposite, such as

\$\$\$6
\$\$6\$
\$6\$\$
6\$\$\$
• 02-12-2002
Prelude
Just reverse the loop values.
Code:

```#include <stdio.h> #include <stdlib.h> int main ( void ) {   int i, j, max;   printf ( "Enter a number: " );   scanf ( "%d", &max );   for ( i = 0; i <= max; ++i ) {     for ( j = max; j >= 0; --j ) {       if ( j == i )         printf ( "%d", max );       else         printf ( "\$" );     }     printf ( "\n" );   }   return EXIT_SUCCESS; }```
-Prelude
• 02-13-2002
dmboasisu2
I'm trying to get this pattern

\$\$\$\$
\$\$\$4
\$\$4\$
\$4\$\$

Here is my code:

It prints out this (\$\$\$\$\$\$\$4\$\$44\$444) which is totally wrong,
instead of what it looks like above

#include <stdio.h>

int main()
{
int limit;
int row;
int col;
int var;
int dar;

printf("\nPlease enter a number between 2 and 9: ");
scanf("%d", &limit);

printf("\n");

for(row=1; row<=limit; row++)
if(row==1){
for(col=1; col<=limit; col++)
printf("\$");
}
else if (row > 1) {
for(var = limit; var >= row; var--)
printf("\$");
for(dar = 1;dar < row; dar++)
printf("%d", limit);
}
else
printf("\$");
printf("\n");
return 0;
}

Any help would be great.
• 02-13-2002
Unregistered
Sniff Sniff S215???? YUPPPPPPPPPPPP
• 02-13-2002
SavesTheDay
.....the code i wrote along time ago (which you never responded too) easily does this backwards too:

Code:

```#include <stdio.h> int main (void) {         int limit,x,col=1;         printf("\n\tEnter a Number Between 2 and 9:  ");         scanf("%i", &limit);         printf("\n\t");         while (col<=limit)         {                 for (x=limit; x>0; x--)                 {                         printf("\$");                         if ((col-1)==x)                                 {                                                        printf("\b%d",limit);                                 }                         }                 col++;                 printf("\n\t");         }         return 0; }```
I think you're trying too hard....anyways if you want to USE your code...one problem is you are missing an important "\n"....

Code:

```else if (row > 1) {         printf("\n"); for(var = limit; var >= row; var--) printf("\$"); for(dar = 1;dar < row; dar++) printf("%d", limit); }```

By throwing in that \n, the code will atleast chop the lines correctly...although JUST doing that, your code still messes up somewhat and doesn't print the right characters towards the bottom....your code looks confusing...take a look at mine, it uses much less confusion and variables.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last