Hi
This code:
int i, j, b;
b = 2;
for (i = 1; i <=3; i++)
{
for (j = 1; j <= i; j++)
Console.Write("{0} ", b++);
Console.Write("\n");
}
provides this:
2
3 4
5 6 7
How can i increment the 'b' value by 2 instead of by 1 but showing the first value (2) first?
I tried this:Console.Write("{0} ", b+=2);
but this gives
4
6 8
10 12 14
I want this:
2
4 6
8 10 12
Thanks

Arunprasad T S V48 reputationPosted Oct 27, 2021, 7:38 AM EDT
Initiate b value with zero.
try the following code,
int i, j, b;
b = 0;
for (i = 1; i <=3; i++)
{
for (j = 1; j <= i; j++)
{
b=b+2;
Console.Write("{0} ", b);
}
Console.Write("\n");
}
Ketan MokariyaRank #1,793 · 832 reputationPosted Oct 27, 2021, 7:52 AM EDT
Kirtesh ShahRank #250 · 10K reputationPosted Oct 27, 2021, 7:54 AM EDT
Hi,
Please try this soultion,
int i, j, b;
b = 0;
for (i = 1; i <= 5; i++)
{
for (j = 0; j < i; j++)
{
b = b + 2;
Console.Write(b + "\t");
}
Console.Write("\n");
}
Console.ReadLine();
Output
Valerie MeunierRank #1,527 · 1K reputationPosted Oct 27, 2021, 8:00 AM EDT