3
07/11/2024 2:06 pm
Aiheen aloittaja
In C# I need to write a console application (small program) that prints the first 10 members of the sequence: 2, -3, 4, -5, 6, -7, 8, -9, 10, -11
2 Vastaukset
2
07/11/2024 2:06 pm
You can use the for cycle:
using System;
class PrintSequenceNumbers
{
static void Main()
{
int numberPrint;
for (int i = 2; i <= 11; i++)
{
if (i % 2 == 0)
{
numberPrint = i;
}
else
{
numberPrint = i * (-1);
}
Console.WriteLine(numberPrint);
}
}
}
1
07/11/2024 2:07 pm
For longer number sequence:
using System;
class PrintLongSequence
{
static void Main()
{
Console.BufferHeight = 2000;
int numberToPrint;
for (int i = 2; i <= 1001; i++)
{
if (i % 2 == 0)
{
numberToPrint = i;
}
else
{
numberToPrint = i * (-1);
}
Console.WriteLine(numberToPrint);
}
}
}
