In dãy Fibonacci trong C# - Tìm dãy số Fibonacci - Lập trình C# - Loop trong C#
Cho chuỗi fibonaci sau
F(n) = f(n-1) + f(n-2)
F(0) = 1
F(1) = 1
Nhập vào số max -> thực hiện in ra các số fibonaci có giá trị < max
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Trần Việt Đức Anh [C2010L]](https://www.gravatar.com/avatar/caee507e11365ca2cf5068cbea5c740a.jpg?s=80&d=mm&r=g)
Trần Việt Đức Anh
2021-10-02 10:42:37
#Ex1397
/* Cho chuỗi fibonaci sau
F(n) = f(n-1) + f(n-2)
F(0) = 1
F(1) = 1
Nhập vào số max -> thực hiện in ra các số fibonaci có giá trị < max
*/
using System;
namespace Ex1397
{
class Program
{
static void Main(string[] args)
{
int max;
max = Convert.ToInt32(Console.ReadLine());
int a = 1, b = 1,
sum = a + b;
if (max == 2)
Console.WriteLine("1 1");
else
{
Console.Write("1 1");
while(sum < max)
{
Console.Write("{0} ", sum);
a = b;
b = sum;
sum = a + b;
}
}
}
}
}
![hieuvm0512 [community,C2010L]](https://www.gravatar.com/avatar/0cacbf21fed14b987a433597d2edc14f.jpg?s=80&d=mm&r=g)
hieuvm0512
2021-10-02 07:54:40
using System;
namespace Fibo
{
class Program
{
static void Main(string[] args)
{
int n;
Console.WriteLine("Nhap vao N");
n = Convert.ToInt32(Console.ReadLine());
for(int i = 0; i<n; i++)
{
Console.Write(fibonacci(i)+ " ");
}
}
public static int fibonacci(int i)
{
if (i < 0)
{
return -1;
}
if(i == 0|| i == 1)
{
return i;
}
else
{
return fibonacci(i - 1) + fibonacci(i - 2);
}
}
}
}
![Đào Mạnh Dũng [C2010L]](https://www.gravatar.com/avatar/6a111fa53fd75dc87034660a8857df16.jpg?s=80&d=mm&r=g)
Đào Mạnh Dũng
2021-09-30 14:01:25
using System;
class fibonaci
{
public static void Main()
{
Console.WriteLine("nhap max : ");
int Max = int.Parse(Console.ReadLine());
int[] fibonaci = new int[Max];
if (Max < 1)
{
return;
}
else
{
if (Max == 1)
{
Console.WriteLine("1 1");
}
else
{
Console.Write("1");
fibonaci[0] = 1;
fibonaci[1] = 1;
int i = 1;
while (fibonaci[i]<= Max)
{
Console.Write(" " + fibonaci[i]);
i++;
fibonaci[i] = fibonaci[i - 1] + fibonaci[i - 2];
}
}
}
}
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-05-18 04:45:55
using System;
namespace fibonaci
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Nhap so N:");
int N = Int32.Parse(Console.ReadLine());
int f0 = 1;
int f1 = 1;
int S = 0;
while (S < N)
{
Console.WriteLine(S);
S = f0 + f1;
Console.WriteLine("");
f0 = f1;
f1 = S;
}
}
}
}
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2021-05-17 08:21:00
using System;
namespace bai1397
{
class DayFibo
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
int f0 = 0, f1 = 1, fn, max;
Console.WriteLine("Nhap max = ");
max = Int32.Parse(Console.ReadLine());
Console.WriteLine(f0 + " " + f1 + " " + 1);
fn = f0 + f1;
while (fn <= max)
{
f0 = f1;
f1 = fn;
fn = f0 + f1;
if (fn > max)
{
break;
}
Console.WriteLine(" " + fn + " ");
}
}
}
}
![Trần Văn Lâm [T2008A]](https://www.gravatar.com/avatar/cfc15c8cb7781ad669b013e01f9f1a6b.jpg?s=80&d=mm&r=g)
Trần Văn Lâm
2021-05-15 14:09:34
namespace LearnCsharpvip2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Nhap n =");
int n = Convert.ToInt32(Console.ReadLine());
if (n >= 0) {
long f0 = 0, f1 = 1, fn = 0;
if(n < 2)
{
fn = n;
}
for(int i = 2; i <= n; i++)
{
fn = f0 + f1;
f0 = f1;
f1 = fn;
}
Console.WriteLine($"F{n} = {fn}");
}
else
{
Console.WriteLine("Hay nhap N");
}
}
}
}
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2021-05-14 09:42:44
using System;
namespace ptb1
{
internal class Program
{
private static void Main(string[] args)
{
int n;
n = Convert.ToInt32(Console.ReadLine());
int a = 1, b = 1, sum=a+b;
if (n == 2) Console.Write("1 1");
else
{
Console.Write("1 1 ");
while(sum < n)
{
Console.Write("{0} ", sum);
a = b;
b = sum;
sum = a + b;
}
}
}
}
}
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2021-05-14 09:07:53
using System;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int f1 = 0, f2 = 1;
int f = 1, max;
Console.WriteLine("Nhap max: ");
max = Int32.Parse(Console.ReadLine());
Console.WriteLine("day fibonacci: {0}", max);
for (int i = 0; f < max - f/2; i++)
{
f = f1 + f2;
f1 = f2;
f2 = f;
Console.WriteLine(", {0}", f);
}
Console.ReadKey();
}
}
}
![Nguyễn đình quân [T2008A]](https://www.gravatar.com/avatar/46aca6afcfe99fdb28357afb847d8a0c.jpg?s=80&d=mm&r=g)
Nguyễn đình quân
2021-05-14 08:59:58
using System;
namespace Lession3
{
class DisplayFibonacci
{
static void Main(string[] args)
{
int f1 = 1, f2 = 1;
int max, f = 1;
Console.Write("Nhap so max : ");
max = int.Parse(Console.ReadLine());
Console.Write("Day so fibonacci tu -> {0} : 0 , 1 , 1 ", max);
for (int i = 0; f < max - f / 2; i++)
{
f = f1 + f2;
f1 = f2;
f2 = f;
Console.Write(", {0} ", f);
}
Console.ReadKey();
}
}
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-05-14 08:57:09
using System;
namespace baitap
{
class Program
{
static void Main(string[] args)
{
int max;
Console.WriteLine("Nhap so max");
max = Int32.Parse(Console.ReadLine());
if (max < 0)
{
Console.WriteLine("Nhap lai !");
}
else {
int f0 = 0;
int f1 = 1;
int fn ;
int i;
Console.WriteLine("Day Fibonacci :");
Console.WriteLine(f0);
Console.WriteLine(f1);
for (i = 2; i < max; i++) {
fn = f1 + f0;
Console.WriteLine(fn);
f0 = f1;
f1 = fn;
}
}
}
}
}