Q11 write a windows application to print series of fibonaaci?

To get this series we have to add first two numbers of series to get the 3rd digit and if we want to get next digit then we have to add last two digits of series means 3rd and 4th. By using this we can get n number of Fibonacci series. The form for this series is as follows:

fibonacci-series

The code is as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CFibo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int a=0,b=1,c=0,n,i;
n = Convert.ToInt32(textBox1.Text);
label2.Text="";
for(i=1;i<=n;i++)
{
label2.Text = label2.Text + " " + a.ToString();
c=a+b;
a=b;
b=c;
}
}
}
}

We have declared a variables a=0, b=1, c, n, I;. Now we have used the label that shows message “Enter n :”; and when we enter any number then this number is read by Convert.ToInt32(textBox1.Text) and assign to n variable.

Here we have used a=0 and b=1 because we want to print 0 and 1 as the 1st and 2nd element of the series. Now we have used for loop and provide a condition i<=n means this loop will be continue till the ith value will not be equal to the entered number. Inside the loop, we have print the variable a and give a tab space. after that we added the a+b and assigned to the c and after that we have assigned b to a ; and c to b. This loop will be continued till the condition will not become false.

The output is as follows:

fibonacci-series