Fibonacci Number

Lardmeister 0 Tallied Votes 497 Views Share

This short C# code will calculate the fibonacci number of an integer and display the result in a Windows GUI label.

// a recursive fibonacci number calculator
// this is a Windows GUI program with a button, textbox and labels
// compiled with C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe

using System;
using System.Windows.Forms;  // GUI stuff

namespace FibonacciSpace
{
   public class FibonacciTesting : System.Windows.Forms.Form
   {
      // designer variables
      private System.ComponentModel.Container components = null;
      private System.Windows.Forms.Button calculateButton;
      private System.Windows.Forms.TextBox inputTextBox;
      private System.Windows.Forms.Label resultLabel;
      private System.Windows.Forms.Label promptLabel;

      public FibonacciTesting()
      {
         // needed for the Windows GUI
         InitializeComponent();
      }

      // as always, clean up any resources being used
      protected override void Dispose( bool disposing )
      {
         if ( disposing )
         {
            if ( components != null ) 
            {
               components.Dispose();
            }
         }
         base.Dispose( disposing );
      }

      // components use in the Windows GUI
      private void InitializeComponent()
      {
         this.inputTextBox = new System.Windows.Forms.TextBox();
         this.calculateButton = new System.Windows.Forms.Button();
         this.promptLabel = new System.Windows.Forms.Label();
         this.resultLabel = new System.Windows.Forms.Label();
         this.SuspendLayout();
         // 
         // inputTextBox
         // 
         this.inputTextBox.Location = new System.Drawing.Point(104, 8);
         this.inputTextBox.Name = "inputTextBox";
         this.inputTextBox.Size = new System.Drawing.Size(100, 20);
         this.inputTextBox.TabIndex = 2;
         this.inputTextBox.Text = "9";
         //
         // calculateButton
         // 
         this.calculateButton.Location = new System.Drawing.Point(10, 40);
         this.calculateButton.Name = "calculateButton";
         this.calculateButton.Size = new System.Drawing.Size(77, 24);
         this.calculateButton.TabIndex = 3;
         this.calculateButton.Text = "Calculate";
         this.calculateButton.Click += new System.EventHandler(this.calculateButton_Click);
         // 
         // promptLabel
         // 
         this.promptLabel.Location = new System.Drawing.Point(10, 8);
         this.promptLabel.Name = "promptLabel";
         this.promptLabel.Size = new System.Drawing.Size(90, 23);
         this.promptLabel.TabIndex = 0;
         this.promptLabel.Text = "Enter an integer:";
         // 
         // resultLabel
         // 
         this.resultLabel.Location = new System.Drawing.Point(104, 40);
         this.resultLabel.Name = "resultLabel";
         this.resultLabel.Size = new System.Drawing.Size(140, 40);
         this.resultLabel.TabIndex = 1;
         // 
         // the Window Form
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
         this.ClientSize = new System.Drawing.Size(255, 85);
         this.Controls.AddRange(new System.Windows.Forms.Control[] {
              this.calculateButton, this.inputTextBox, 
              this.resultLabel, this.promptLabel});
         this.BackColor = System.Drawing.Color.Wheat;  // optional
         this.Name = "FibonacciTesting";
         this.Text = "Fibonacci Number";
         this.ResumeLayout(false);
      }

      static void Main()
      {
         Application.Run( new FibonacciTesting() );
      }

      // call fibonacci and result result
      protected void calculateButton_Click( object sender, System.EventArgs ev )
      {
         int num = Convert.ToInt32( inputTextBox.Text );
         int fibonacciNumber = Fibonacci( num );
         resultLabel.Text = "Fibonacci number is " + fibonacciNumber;
      }

      // calculate fibonacci number of n
      public int Fibonacci( int n )
      {
         if ( n == 0 || n == 1 )
            return n;
         else
            return Fibonacci( n - 1 ) + Fibonacci( n - 2 );
      }
   }
}
nive.s 0 Newbie Poster
private void button1_Click(object sender, System.EventArgs e)
		{
			int num1=0;
			int num2=1;
			int limit;
			limit = int.Parse(textBox1.Text);
			if(limit==0 || limit==1)
			{
				textBox2.Text = num1.ToString();
			}
			else
				textBox2.Text = num1.ToString()+" "+num2.ToString();

			for(int fib=2;fib<limit;fib++)
			{
				int nextnum=num1+num2;
				textBox2.Text=textBox2.Text+" "+nextnum.ToString();
				num1=num2;
				num2=nextnum;
			}
			
		}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.