Making a pyramid | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Making a pyramid

Hello, static void DrawPyramid(int n) { for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { Console.Write(" "); } for (int k = 1; k <= 2 * i - 1; k++) { Console.Write("*" + " "); } Console.WriteLine(); } } According to my calculations, this shouldnt work, yet it does... Especially this line confuses me: for (int k = 1; k <= 2 * i - 1; k++) Assume I enter 1 for n, then through the process i becomes 2. 2x2-1=3. That means, for n=1 there should be 3x* in the Console. I don't get it.

12th Jan 2017, 4:28 PM
Eric
1 Réponse
+ 2
If n is 1, the outer loop condition will ensure that the loop is executed only once for i=1. So, nothing will be printed for i=2.
13th Jan 2017, 5:01 AM
Igor B
Igor B - avatar