Increasing Number Triangle Starting from 0 in C#

What You’ll Learn
How to generate each value as i + j while printing a triangle from i = 0 to 5.
⭐ Pattern Output
For rows = 6 (i from 0 to 5), the pattern looks like this:
Output
0\n1 2\n2 3 4\n3 4 5 6\n4 5 6 7 8\n5 6 7 8 9 101
Complete C# Program
This follows the reference program: loop i from 0 to 5 and print (i + j) for j = 0..i.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
for (i = 0; i <= 5; i++)
{
for (j = 0; j <= i; j++)
Console.Write((i + j) + " ");
Console.WriteLine();
}
}
}
}2
Variation — User Input max row
Let the user choose the maximum row value (the last i):
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter max i: ");
int max = Convert.ToInt32(Console.ReadLine());
if (max < 0) return;
for (int i = 0; i <= max; i++)
{
for (int j = 0; j <= i; j++)
Console.Write((i + j) + " ");
Console.WriteLine();
}
}
}
}12 people found this page helpful
