Foros aprenderaprogramar.com
Aprender a programar => C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más => Mensaje iniciado por: César Krall en 24 de Julio 2016, 19:39
-
Ejercicio resuelto en C# (publicado por Dimitar Stefanov):
Fibonacci Numbers
Description
Write a program that reads a number N and prints on the console the first N members of the Fibonacci sequence (at a single line, separated by comma and space - ", ") : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ….
El código:
using System;
namespace FibonacciNumbers {
class Program {
static void Main() {
int cantidad = int.Parse(Console.ReadLine());
long a = 0;
long b = 1;
long aux = 0;
for(int i = 0;i<cantidad;i++) {
if(i==0) {
Console.Write("{0}", a);
}else {
aux = a;
a = b;
b = aux + a;
Console.Write(", {0}",a);
}
}
Console.WriteLine();
}
}
}
Saludos.