# nested for loop triangle in c

• (2 Pages)
• 1
• 2

## 16 Replies - 33540 Views - Last Post: 26 February 2010 - 01:57 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=158511&amp;s=2a0e6d9fffe4cc50457e95c163c6b5e2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 aleeza

Reputation: 0
• Posts: 12
• Joined: 26-February 10

# nested for loop triangle in c

Posted 26 February 2010 - 10:49 AM

actually i am novice in c and c++ and i start using for loop and seeking an output like this:
54321
.4321
..321
...21
....1

```#include<stdio.h>
#include<conio.h>
void main(void)
{
for(int i=5;i>=1;i--)
{
for(int j=5;j<=i-1;j--)
{
printf("%d",j);
printf("\n");
}
}
getch();
}

```

so far i've come up with this code em kind ov confused in j<=i-1 this area need help an explanation will b highly appreciated
tiger007

This post has been edited by NickDMax: 26 February 2010 - 12:02 PM
Reason for edit:: fixed formatting

Is This A Good Question/Topic? 0

## Replies To: nested for loop triangle in c

### #2 no2pencil

• Professor Snuggly Pants

Reputation: 6635
• Posts: 30,897
• Joined: 10-May 07

## Re: nested for loop triangle in c

Posted 26 February 2010 - 11:30 AM

This code creates the following output :

```#include <stdio.h>

#define MAX 6

int main(void) {
char str[6]="54321";
int i=0,j=5;
for(i=MAX;i>=0;i--) {
for(j=0;j<=i;j++) {
printf("%c",str[j]);
}
printf("\n");
}
return 0;
}

```

Quote

~/code/c \$ ./output
54321
54321
54321
5432
543
54
5

It's close to what you are looking for

### #3 Ferencn

• D.I.C Regular

Reputation: 71
• Posts: 322
• Joined: 01-February 10

## Re: nested for loop triangle in c

Posted 26 February 2010 - 11:35 AM

Do you mean an output like this:
```54321
4321
321
21
1
```

or this:
```54321
4321
321
21
1
```

This post has been edited by Ferencn: 26 February 2010 - 11:36 AM

### #4 Ferencn

• D.I.C Regular

Reputation: 71
• Posts: 322
• Joined: 01-February 10

## Re: nested for loop triangle in c

Posted 26 February 2010 - 11:42 AM

```void main(void)
{
for(int i=5;i>=1;i--)
{
for(int j=5;j<=i-1;j--) // <--- j will never be <= i-1, so this for loop will never execute!
// i starts at 5 and decrements, so i-1 starts at 4 and decrements.
// j is starting at 5, so j will always be bigger than i-1!!!
{
printf("%d",j);
printf("\n");
}
}
getch();
}

```

### #5 aleeza

Reputation: 0
• Posts: 12
• Joined: 26-February 10

## Re: nested for loop triangle in c

Posted 26 February 2010 - 12:24 PM

actually i need an output like this
54321
.4321
..321
...21
....1

yeah

This post has been edited by NickDMax: 26 February 2010 - 12:34 PM
Reason for edit:: moderator to the rescue!

### #6 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: nested for loop triangle in c

Posted 26 February 2010 - 12:32 PM

aleeza, our forums compress spaces and remove any leading spaces when (actually it is HTML that does this, but that is another topic).

To get formatted text to display correctly you can use code tags: [code]... your text/code here... [/code]

Or you can get creative with some of the other BBCode tags available.

For example I to format your post above I used the technique I talked about here on my blog

### #7 aleeza

Reputation: 0
• Posts: 12
• Joined: 26-February 10

## Re: nested for loop triangle in c

Posted 26 February 2010 - 12:34 PM

so can i thought this decrement logic will work
can u pleeeez suggest and explain me this...

### #8 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: nested for loop triangle in c

Posted 26 February 2010 - 12:41 PM

Here is an example that is *almost* what you need. I intentionally left it off a little so that you can do *something*

```#include<stdio.h>

int main() {
int i, j;
for(i = 5; i> 0; --i) {
for(j = 5 - i; j > 0; --j) { printf(" "); }
for(j = 1; j < i+1; ++j) { printf("%d", j); }
printf("\n");
}
return 0;
}
```

here the output is:
12345
.1234
..123
...12
....1

not quite what you need but you should be able to see how to finish it up.

### #9 no2pencil

• Professor Snuggly Pants

Reputation: 6635
• Posts: 30,897
• Joined: 10-May 07

## Re: nested for loop triangle in c

Posted 26 February 2010 - 12:44 PM

I like using the string array

### #10 Ferencn

• D.I.C Regular

Reputation: 71
• Posts: 322
• Joined: 01-February 10

## Re: nested for loop triangle in c

Posted 26 February 2010 - 12:51 PM

What you want to do is:
starting at 5, for trianglesize 5:
5: 0 spaces, then 5,4,3,2,1
4: 1 space, then 4,3,2,1
3: 2 spaces, then 3,2,1
2: 3 spaces, then 2,1
1: 4 spaces, then 1
As you can see, the number of spaces increases, while the number of digits decreases.
so you want to print # of digits lines, decreasing, each with the number of spaces increasing:

```for (int numdigits = 5;numdigits >0;numdigits --)
{
// each line we want to print 5-numdigits spaces!
for (int spaces = 0;spaces < (5-numdigits); spaces++)
printf(" ");
// now print the digits themselves, starting at the highest (numdigits) and ending with 1:
for (int outdigit=numdigits; outdigit>0;outdigit--)
printf("%d",outdigit);
// finally a newline, so the next line starts at the beginning:
printf("\n");
}
// DONE!

```

no2pencil, on 26 February 2010 - 07:44 PM, said:

I like using the string array

Your stringarray approach is very nice as well! especially if you want to print something like:
```pencil
encil
ncil
cil
il
l
```

instead of sequential numbers or characters!

This post has been edited by Ferencn: 26 February 2010 - 12:49 PM

### #11 no2pencil

• Professor Snuggly Pants

Reputation: 6635
• Posts: 30,897
• Joined: 10-May 07

## Re: nested for loop triangle in c

Posted 26 February 2010 - 12:54 PM

Yeah, through the loop you could just set the array element to space, rather than fussing with another loop & variable counter.

### #12 Ferencn

• D.I.C Regular

Reputation: 71
• Posts: 322
• Joined: 01-February 10

## Re: nested for loop triangle in c

Posted 26 February 2010 - 12:58 PM

no2pencil, on 26 February 2010 - 07:54 PM, said:

Yeah, through the loop you could just set the array element to space, rather than fussing with another loop & variable counter.

Thus proving that there is no 'best' algorithm

### #13 aleeza

Reputation: 0
• Posts: 12
• Joined: 26-February 10

## Re: nested for loop triangle in c

Posted 26 February 2010 - 12:58 PM

yeah thanxxx alot n i got a little bit
i need 2 ask u something the second loop which is
for(j=5-i;j>0;--j)
is for the decrement of spaces am i right???
and for(j=1;j<i+1;++j)
is for printing j from 1 to 5 right??

This post has been edited by aleeza: 26 February 2010 - 01:02 PM

### #14 Ferencn

• D.I.C Regular

Reputation: 71
• Posts: 322
• Joined: 01-February 10

## Re: nested for loop triangle in c

Posted 26 February 2010 - 01:05 PM

Correct. But, for the next line it will print one more space and then 1 to 4... etc.

This post has been edited by Ferencn: 26 February 2010 - 01:07 PM

### #15 aleeza

Reputation: 0
• Posts: 12
• Joined: 26-February 10

## Re: nested for loop triangle in c

Posted 26 February 2010 - 01:22 PM

Ferencn, on 26 February 2010 - 12:05 PM, said:

Correct. But, for the next line it will print one more space and then 1 to 4... etc.

print one more space???
sorrryyy bt didn get u little more explanation pleeez

NickDMax, on 26 February 2010 - 11:41 AM, said:

Here is an example that is *almost* what you need. I intentionally left it off a little so that you can do *something*

```#include<stdio.h>

int main() {
int i, j;
for(i = 5; i> 0; --i) {
for(j = 5 - i; j > 0; --j) { printf(" "); }
for(j = 1; j < i+1; ++j) { printf("%d", j); }
printf("\n");
}
return 0;
}
```

here the output is:
12345
.1234
..123
...12
....1

not quite what you need but you should be able to see how to finish it up.

THANX:)