Increasing Number Triangle Using i + j - 1 in C#

What You’ll Learn
How to compute each value using i + j - 1 to generate consecutive numbers on every row.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
Output
1\n2 3\n3 4 5\n4 5 6 7\n5 6 7 8 91
Complete C# Program
This follows the reference program exactly: print (i + j - 1) for j = 1..i.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
Console.Write((i + j - 1) + " ");
Console.WriteLine();
}
}
}
}2
Variation — User Input rows
Let the user choose the number of rows:
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter rows: ");
int rows = Convert.ToInt32(Console.ReadLine());
if (rows < 1) return;
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
Console.Write((i + j - 1) + " ");
Console.WriteLine();
}
}
}
}12 people found this page helpful
