Home > Blockchain >  How to add the current iteration's value to the previous iteration's value in a loop C#
How to add the current iteration's value to the previous iteration's value in a loop C#

Time:06-11

I can find a Java version for my question, but not C#. My current attempt goes crazy when attempting to add them. I feel like there is a simple fix, but I'm struggling to think of it.

// See https://aka.ms/new-console-template for more information
int[] input = { 28, 2, 3, -3, -2, 1, 2, 35, -1, 0, 0, -1 };
for (int i = 0; i < input.Length; i  )
{
    int x = input[i];
    int y = input[i  ];
    int output = x   y;
    Console.WriteLine(output);
}

CodePudding user response:

If I understand your task answer is (updated):

var sum = 0;
var input = new int[] { 28, 2, 3, -3, -2, 1, 2, 35, -1, 0, 0, -1 };
for (var i = 0; i < input.Length - 1; i  )
{
    sum  = input[i];
    Console.WriteLine(sum);
}

CodePudding user response:

This is the correct code :

int[] input = { 28, 2, 3, -3, -2, 1, 2, 35, -1, 0, 0, -1 };
for (int i = 0; i < input.Length - 1; i  )
{
    int x = input[i];
    int y = input[i   1];
    int output = x   y;
    Console.WriteLine(output);
}
  •  Tags:  
  • c#
  • Related