Fibonacci C# for loop
C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace demo
{
class Program
{
public static int functionDemo(int n)
{
if((n==0 ) || (n ==1)){
return n;
}
else
{
return functionDemo(n - 1) + functionDemo(n - 2);
}
}
static void Main(string[] args)
{
for (int i = 0; i < 15; i++ )
{
Console.WriteLine(functionDemo(i));
}
Console.Read();
}
}
}
Fibonacci C# for loop
C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace demo
{
class Program
{
public static int Fibonacci(int n)
{
int a = 0;
int b = 1;
// In N steps compute Fibonacci sequence iteratively.
for (int i = 0; i < n; i++)
{
int temp = a;
a = b;
b = temp + b;
}
return a;
}
static void Main()
{
for (int i = 0; i < 15; i++)
{
Console.WriteLine(Fibonacci(i));
}
Console.Read();
}
}
}
Fibonacci C# for loop
C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace demo
{
class Program
{
static void Main()
{
for (int a = 0, b = 1; a < 400; a = a + b, b = a - b)
{
Console.WriteLine(a + " ");
}
Console.Read();
}
}
}
Fibonacci C# while loop
C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace demo
{
class Program
{
public static void change(int a, int b)
{
while (a <= 300)
{
a = b + a;
b = a - b;
Console.WriteLine(a);
}
Console.Read();
}
static void Main()
{
change(0, 1);
}
}
}
Fibonacci C# do while loop
C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace demo
{
class Program
{
public static void change(int a, int b)
{
do{
a = b + a;
b = a - b;
Console.WriteLine(a);
} while (a <= 300) ;
Console.Read();
}
static void Main()
{
change(0, 1);
}
}
}
Fibonacci C# do while loop
C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace demo
{
class Program
{
static void Main(string[] args)
{
int a = 0, b = 1, sum = 1;
while (a < 150)
{
sum = a + b;
a = b;
b = sum;
Console.WriteLine(sum);
}
Console.Read();
}
}
}
0 nhận xét:
Post a Comment