[Solved] function to calculate the sum of odd numbers in a given stack [closed]


just an example, you could pass your stack variable as an argument to the GetSum() function.

private static int GetSum()
        {
              Stack<int> stack = new Stack<int>();

            stack.Push(2);
            stack.Push(5);
            stack.Push(7);
            stack.Push(4);
            stack.Push(1);

            int sum = 0;

            foreach (int number in stack)
            {
                if (number % 2 != 0)
                {
                    sum += number;
                }
            }

        return sum;
    }

2

solved function to calculate the sum of odd numbers in a given stack [closed]