Kenan Balija
Kenan Balija

Reputation: 703

c# for Count to maximum array.Length?

Can u help a fellow noob programmer. I will write the code first:

static void Main(string[] args)
{
    int m = int.Parse(Console.ReadLine());
    int[] a = new int[m];

    for (int i = 1; i < m; i++)
    {
        a[i] = i * 5;
        Console.WriteLine(a[i]);
    }
}

So the output here after i write 2 in input is: "5,10" What do I add for output to be: "5 ,10, 15" (without writing 3 in input)

Upvotes: 0

Views: 60

Answers (1)

Jon Tirjan
Jon Tirjan

Reputation: 3694

Your for loop needs to start at 0. Arrays (like most collections) use 0-based indexing.

for (int i = 0; i < m; i++)
{
    a[i] = (i + 1) * 5;
    Console.WriteLine(a[i]);
}

Upvotes: 3

Related Questions