Hello all,
Well i wanted a output of a prorgram to look like this, i tried but can't figured out the logic, can anyone plesae help me out here with this
1
232
34543
4567654
567898765
Hello all,
Well i wanted a output of a prorgram to look like this, i tried but can't figured out the logic, can anyone plesae help me out here with this
1
232
34543
4567654
567898765
Effort required.
yes mate i tried from my side, but needed some expert here
One way to tackle it is to think about the shape first. You know its a triangle so its going to need for loops, more than likely nested loops. Within each increment of the inner loop calculate/print the number.
If you are having trouble, think about the pescudo code first, or plan it out on paper.
Double Helix STL
thankz for your suggesetion M8, i have figured it out that there will be nested looping in this, but can't get to the logic of looping
These types of things are interesting. Look at this table:
(0 represents space or padding)
000010000
000232000
003454300
045676540
567898765
What do we need to know. We need to know how much padding or space to provide, how many true digits to print, and what those digits should be.
1. The "row" is going to go from 1 to 5;
2. The "pad" is going to go from 4 to 0;
3. The maximum value in the row is equal to the total number of digits printed.
Look at this table:
row|pad|max
1 4 1
2 3 3
3 2 5
4 1 7
5 0 9
What can we learn:
1. pad = 5 - row;
2. max = (2 * row) -1;
Here is how to do it, step by step.
Code:/* Outer loop */ for(row = 1; row <= 5; ++ row){ /* The pad loop. notice that as row increments the number of spaces printed decrements */ for(pad = row; pad < 5; ++ pad) printf(" "); /* Now we are going to print the digits. This requires two loops. One that increments to "max", and one decrements back to "row */ max = (2 * row) -1; for(col = row; col <= max; ++ col) printf("%d",col); for(col = max - 1; col >= row; -- col) printf("%d",col); /* Finish with a newline */ printf("\n"); }
Ok... well think of the math involved a little more. Think about the fact that the first number is going to be centered and the space on each side is going to be equal (which isn't necessarily as relavant in this project since you only need to write spaces before your data, not the spaces afterward.