settingsAccountsettings
Menusettings

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

+5 votes

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

asked in C# category by user Jolie Ann

2 Answers

+3 votes
 
Best answer

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);
        }
    }
}
answered by user nikole
edited by user golearnweb
+2 votes

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);
        }
    }
}
answered by user Jolie Ann
edited by user golearnweb
...