Click here to Skip to main content
15,921,622 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please print this number pattern using nested loop

1
3 1
5 3 1
7 5 3 1
9 7 5 3 1
Posted
Comments
[no name] 29-Apr-13 8:38am    
Please do your own homework
Richard MacCutchan 29-Apr-13 8:53am    
OK, I've done that. What's next?

We don't do your homework (even really simple homework, like yours...).
Let's try yourself and post here specific questions when you get stuck.
 
Share this answer
 
It does not work like this here.

Here is what is expected of enquirers:
1. TRY first what you want to do! You may find that it's not that hard.
2. Formulate what was done by you that looks like an issue/not working.

Try them and tell if you face issues.
Members will be more than happy to help like this.
 
Share this answer
 
You start by counting down from one to one.
The next line you start counting down from 3 to 1 with a step of two.
The next line you start counting down from 5 to 1 with a step of two.
The next line you start counting down from 7 to 1 with a step of two.
The next line you start counting down from 9 to 1 with a step of two.

Lets summarize what's going on here:

1. The end value of each row is always one
2. 3 - 1 = 2 and 5 - 3 = 2 and 7 - 5 = 2 and 9 -7 = 2. This tells us that the start value of the for loop gets incremented by 2 for each line.
3. There is no line whith a start value greater than nine (9). So lets just stop this mess when the start value gets bigger than nine or just use an outer loop around the inner loop. One loop counts up by increments of two and the other counts down by decrements of two. You go and figure out which one does which. It isn't too hard, you just give it a try.

Regards,

— Manfred
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900