Fibonacci Series:
Fibonacci series is the sequence of the numbers which has following properties:- First number is 1.
- Second number is 1
- Remaining other numbers are sum of their preceding numbers. Eg: third number = 1+1=2
- Fibonacci series: 1, 1, 2, 3, 5, 8, 13, ....
------------------------------------------------------------------
//Source code using iteration method
using System;
public class Program_Fibonacci_Iteration
{
public static void Main()
{
int num;
long ans;
Console.Write("Enter any number: ");
num = Convert.ToInt32(Console.ReadLine());
ans = Fibo(num);
Console.WriteLine("\n :::::::: OUTPUT :::::::::\n");
Console.WriteLine("The " + num + " th number in fibonacci series is: \n" + ans);
Console.WriteLine("\n\nPress any key to exit...");
Console.ReadLine();
}
static int Fibo(int n)
{
int num1 = 1, num2 = 1;
int num3 = 0;
if (n <= 2)
return 1;
else
{
for (int i = 3; i <= n; i++)
{
num3 = num1 + num2;
num1 = num2;
num2 = num3;
}
return num3;
}
}
}
OUTPUT
Enter any number: 8
:::::::: OUTPUT :::::::::
The 8 th number in fibonacci series is:
21
Press any key to exit...
-------------------------------------------------------------------
//Source code using recursion method
using System;
public class Program_Fibonacci_Recursion
{
public static void Main()
{
int num;
long ans;
Console.Write("Enter any number: ");
num = Convert.ToInt32(Console.ReadLine());
ans = Fibo(num);
Console.WriteLine("\n :::::::: OUTPUT :::::::::\n");
Console.WriteLine("The " + num + " th number in fibonacci series is: \n" + ans);
Console.WriteLine("\n\nPress any key to exit...");
Console.ReadLine();
}
static int Fibo(int n)
{
if (n <= 2)
return 1;
else
return (Fibo(n-1) + Fibo(n-2));
}
}
OUTPUT
Enter any number: 8
:::::::: OUTPUT :::::::::
The 8 th number in fibonacci series is:
21
Press any key to exit...
---------------------------------------------------------------
//Printing Fibonacci Series
using System;
public class Program_Fibonacci_Recursion
{
public static void Main()
{
int n, num1, num2, num3;
num1 = 1; //First fibonacci number
num2 = 1; //Second fibonacci number
Console.Write("Enter any number: ");
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n\n:::::::: OUTPUT :::::::::");
//To print first and second fibonacci numbers
Console.WriteLine("Position: " + 1 + "\tValue: " + num1);
Console.WriteLine("Position: " + 2 + "\tValue: " + num2);
//Counting for fibonacci number starts from third, so i = 3 is assigned
for (int i = 3; i <= n; i++)
{
num3 = num1 + num2;
num1 = num2;
num2 = num3;
Console.WriteLine("Position: " + i + "\tValue: " + num3);
}
Console.WriteLine("\n\nPress any key to exit...");
Console.ReadLine();
}
}
---------------------------------------------------------------
//Printing Fibonacci Series
using System;
public class Program_Fibonacci_Recursion
{
public static void Main()
{
int n, num1, num2, num3;
num1 = 1; //First fibonacci number
num2 = 1; //Second fibonacci number
Console.Write("Enter any number: ");
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n\n:::::::: OUTPUT :::::::::");
//To print first and second fibonacci numbers
Console.WriteLine("Position: " + 1 + "\tValue: " + num1);
Console.WriteLine("Position: " + 2 + "\tValue: " + num2);
//Counting for fibonacci number starts from third, so i = 3 is assigned
for (int i = 3; i <= n; i++)
{
num3 = num1 + num2;
num1 = num2;
num2 = num3;
Console.WriteLine("Position: " + i + "\tValue: " + num3);
}
Console.WriteLine("\n\nPress any key to exit...");
Console.ReadLine();
}
}
OUTPUT
Enter any number: 12
:::::::: OUTPUT :::::::::
Position: 1 Value: 1
Position: 2 Value: 1
Position: 3 Value: 2
Position: 4 Value: 3
Position: 5 Value: 5
Position: 6 Value: 8
Position: 7 Value: 13
Position: 8 Value: 21
Position: 9 Value: 34
Position: 10 Value: 55
Position: 11 Value: 89
Position: 12 Value: 144
Press any key to exit...
No comments:
Post a Comment