Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 25th, 2012  |  syntax: C#  |  size: 1.14 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2.  
  3. namespace Cosas
  4. {
  5.         class Program
  6.         {
  7.                 static void Main (string [] args)
  8.                 {
  9.                         int num;
  10.  
  11.                         Console.Write ("Ingresa tu valor n: ");
  12.                         num = Int32.Parse (Console.ReadLine ());
  13.  
  14.                         int [] valores = Fibonacci (num);
  15.  
  16.                         Console.WriteLine ("La serie tiene {0} nĂºmeros pares menores a {1}", ContarPares (valores, num), num);
  17.                         Console.WriteLine ("La suma de sus elementos es: {0}", SumarValores (valores));
  18.  
  19.                         Console.ReadKey ();
  20.                 }
  21.  
  22.                 public static int [] Fibonacci (int n)
  23.                 {
  24.                         if (n == 0) return new int [] { n };
  25.  
  26.                         int [] valores = new int [n + 1];
  27.  
  28.                         valores [0] = 0;
  29.                         valores [1] = 1;
  30.  
  31.                         for (int i = 2; i <= n; i++)
  32.                         {
  33.                                 valores [i] = valores [i - 1] + valores [i - 2];
  34.                         }
  35.  
  36.                         return valores;
  37.                 }
  38.  
  39.                 public static int ContarPares (int [] arreglo, int n)
  40.                 {
  41.                         int cuenta = 0;
  42.  
  43.                         foreach (int num in arreglo)
  44.                         {
  45.                                 if (((cuenta % 2) == 0) && (num < n))
  46.                                 {
  47.                                         cuenta++;
  48.                                 }
  49.                         }
  50.  
  51.                         return cuenta;
  52.                 }
  53.  
  54.                 public static int SumarValores (int [] arreglo)
  55.                 {
  56.                         int suma = 0;
  57.  
  58.                         foreach (int num in arreglo)
  59.                         {
  60.                                 suma += num;
  61.                         }
  62.  
  63.                         return suma;
  64.                 }
  65.         }
  66. }