Resolved I need help, why is not showing the sumOdd(sumaNepar) of fibonacci ?

Beware

New member
Joined
Jun 13, 2023
Messages
2
Programming Experience
Beginner
C#:
namespace Trening
{
    internal class Program
    {
        static void Main(string[] args)
        {         
            int fiboBroevi = 10;
            int[] fibo = new int[fiboBroevi];
            fibo[0] = 0;
            fibo[1] = 1;
// sumaPar = sumEven, sumaNepar = sumOdd
            int sumaPar = 0;
            int sumaNepar = 0;

            for (int i = 2; i < fiboBroevi; i++)
             {
               fibo[i] = fibo[i - 1] + fibo[i - 2];
             }

          
          foreach (int broj in fibo)
          {
                if (broj % 2 == 0)
                {
                    sumaPar += broj;
                }        
                else        
                {
                    sumaNepar += broj;
                }
          }

          Console.WriteLine( sumaPar);
          Console.WriteLine( sumaNepar);
          
        }
    }
}
 
Last edited by a moderator:
What results are you getting? What results were you expecting? If you step through the code with a debugger, do that values match up at each step with your hand calculations that led to your expected results?
 
If the issue is that you are not seeing the Console output, that likely means you are using an older version of Visual Studio which automatically closes the console window when running with the debugger. You can:
  • run without the debugger by pressing Ctrl-F5 (which will hold the console window open);
  • add extra code at the end of your main method to wait for some input; or
  • run your compiled program from the command line.
Personally, I recommend using the latest version of Visual Studio which should default to holding the console window open (unless you change the default settings).
 
You can also set a breakpoint at the end of the Main method so the debugger will halt there are you can see the console output before exiting the app.
 
What results are you getting? What results were you expecting? If you step through the code with a debugger, do that values match up at each step with your hand calculations that led to your expected results?

Result:
sumOdd is shown as 44 .... while need to be 99
Thanks for the help and your effort :)
 
How could it be? The sum of all numbers in your array is not even that high. Put a breakpoint and look at the array and all its values.
 
sumOdd is shown as 44 .... while need to be 99

Recall that C# arrays are zero based... Just like Fibonacci numbers. So your fibo[9] is the same as F9.
 
Back
Top Bottom