# Fibonacci Series Using WPF

Today we will discuss Fibonacci series. Fibonacci series is a series of numbers, i.e., the third number is the sum of the first and second number. Similarly, the fourth number is the sum of the second and third number and so on.

For example, 0 and 1 are the two starting numbers of Fibonacci Series, then the next numbers in the Fibonacci sequence are 1,2,3,5,8,13,21,34,...... Now, we will create a WPF application for calculating the nth term of Fibonacci series.

First, open Visual Studio then File->New->Project. Click on the project then a templates window will appear. Later, select Visual C# template->WPF App(.NET Framework). Then, set the name of the application and location of the application. Then click OK.

Then, from Toolbox select Textblocks, Textboxes, and buttons and set their alignments and properties from Properties window. Then your screen will appear like this.

XAML code for this is,
1. <Window x:Class="Fibonacci_Series_using_WPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Fibonacci_Series_using_WPF" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525">
2.     <Grid>
3.         <TextBlock x:Name="tblname" HorizontalAlignment="Left" Margin="183,31,0,0" TextWrapping="Wrap" Text="Fibonacci Series" VerticalAlignment="Top" Height="25" Width="128" FontSize="16" />
4.         <TextBlock x:Name="tblnum" HorizontalAlignment="Left" Height="24" Margin="152,79,0,0" TextWrapping="Wrap" Text="Enter nth value:" VerticalAlignment="Top" Width="92" />
5.         <TextBlock x:Name="tblres" HorizontalAlignment="Left" Height="24" Margin="152,123,0,0" TextWrapping="Wrap" Text="Result is:" VerticalAlignment="Top" Width="92" FontSize="18" />
6.         <TextBox x:Name="txtnum" HorizontalAlignment="Left" Height="24" Margin="268,79,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="92" />
7.         <TextBox x:Name="txtres" HorizontalAlignment="Left" Height="24" Margin="268,123,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="92" />
8.         <Button x:Name="btncom" Content="Compute" HorizontalAlignment="Left" Margin="152,179,0,0" VerticalAlignment="Top" Width="92" Height="35" FontSize="20" Click="btncom_Click" />
9.         <Button x:Name="btnclr" Content="Clear" HorizontalAlignment="Left" Height="35" Margin="268,179,0,0" VerticalAlignment="Top" Width="92" FontSize="20" Click="btnclr_Click" /> </Grid>
10. </Window>

Source Code for this is
1. using System;
2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Text;
6. using System.Windows;
7. using System.Windows.Controls;
8. using System.Windows.Data;
9. using System.Windows.Documents;
10. using System.Windows.Input;
11. using System.Windows.Media;
12. using System.Windows.Media.Imaging;
14. using System.Windows.Shapes;
15. namespace Fibonacci_Series_using_WPF {
16.     /// <summary>
17.     /// Interaction logic for MainWindow.xaml
18.     /// </summary>
19.     public partial class MainWindow: Window {
20.         public MainWindow() {
21.             InitializeComponent();
22.         }
23.         private void btncom_Click(object sender, RoutedEventArgs e) {
24.             int a = 0, b = 1, total = 0, i;
25.             int n = Convert.ToInt32(txtnum.Text);
26.             for (i = 1; i < n; i++) {
27.                 total = a + b;
28.                 a = b;
29.                 b = total;
30.                 total = a;
31.             }
32.             txtres.Text = total.ToString();
33.         }
34.         private void btnclr_Click(object sender, RoutedEventArgs e) {
35.             txtnum.Text = String.Empty;
36.             txtres.Text = String.Empty;
37.         }
38.     }
39. }

Then run the application.

Outputs

Now, we have created a WPF Application for calculating the nth term of Fibonacci Series.