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 VPosted Oct 27, 2021, 11:38 AM
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");
}
Valerie MeunierPosted Oct 27, 2021, 12:00 PM
Kirtesh ShahPosted Oct 27, 2021, 11:54 AM
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
Ketan MokariyaPosted Oct 27, 2021, 11:52 AM