VD: Nhập n=6 Kq F6: 1 1 2 3 5 8
Main.cs
Program.cs
Main.cs
- Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class fibo
{
public static void Main()
{
int n;
Console.Write("Nhap vao n(n > 0): ");
n = Convert.ToInt32(Console.ReadLine());
bai16 x = new bai16(n);
Console.Write("F{0} = ", n);
x.fibonaci();
Console.Read();
}
}
Program.cs
- Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class bai16
{
int n;
public bai16(int n)
{
this.n = n;
}
public void fibonaci()
{
int i,f1 = 1, f2 = 1, f;
if (n == 1) Console.Write("{0}", f1);
else Console.Write("{0} {1} ", f1, f2);
for (i = 2; i < n; i++)
{
f = f1 + f2;
Console.Write("{0} ", f);
f2 = f1;
f1 = f;
}
}
}