+ 1
please tell me the simple sytax for printing numbera in diamond shape and please help me to understand loop concepts
please teach me loop cocept. i got confused everytime
2 Antworten
+ 2
It's not as hard as it looks...
I assume that you write in c++ and for tge input 4 you want something like this:
*
***
*****
*******
*****
***
*
you have lines, and in each line some characters right?
first, we make a loop for printing evey line.
when user enters n:
for (i = 1; i < (n * 2) -1; i++)
we have n* 2 - 1 lines.
now for each line, we find the number of characters, and using it the number of spaces.
if (i <= n)
count = i * 2 -1;
else if (i > n)
count = ((n * 2) - i) * 2 -1;
so, if you want to make a loop, first you should make an imaginary loop or do it on paper, or write the result. Like when I drew that diamond.
Then, check the number of concepts of what you are going to do, eg: for that diamond we had lines and each line had characters. We create a loop for each.
Then find the relation, to know if the loops are nested or one after another (sequential)
For example, in our diamond example, the loop that wrote characters had to be run in the loop that printed lines. so they had to be nested.
And finally, create the loop. If it's not working, trace it! Run each step of the loop by yourself on a piece of paper and you'll find the problem.
P.S: I'll write that code and post it here for you :)