How to write 10 members of a sequence in C# (C Sharp)?

  

3
Rozpoczęcie tematu

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 Odpowiedzi
2

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

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);
        }
    }
}
Udostępnij: