Skip to content

What is the Fibonacci Sequence and how to use it in C#

[fusion_builder_container hundred_percent=”no” hundred_percent_height=”no” hundred_percent_height_scroll=”no” hundred_percent_height_center_content=”yes” equal_height_columns=”no” menu_anchor=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” status=”published” publish_date=”” class=”” id=”” link_color=”” link_hover_color=”” border_color=”” border_style=”solid” margin_top=”” margin_bottom=”” padding_top=”” padding_right=”” padding_bottom=”” padding_left=”” gradient_start_color=”” gradient_end_color=”” gradient_start_position=”0″ gradient_end_position=”100″ gradient_type=”linear” radial_direction=”center center” linear_angle=”180″ background_color=”” background_image=”” background_position=”center center” background_repeat=”no-repeat” fade=”no” background_parallax=”none” enable_mobile=”no” parallax_speed=”0.3″ background_blend_mode=”none” video_mp4=”” video_webm=”” video_ogv=”” video_url=”” video_aspect_ratio=”16:9″ video_loop=”yes” video_mute=”yes” video_preview_image=”” filter_hue=”0″ filter_saturation=”100″ filter_brightness=”100″ filter_contrast=”100″ filter_invert=”0″ filter_sepia=”0″ filter_opacity=”100″ filter_blur=”0″ filter_hue_hover=”0″ filter_saturation_hover=”100″ filter_brightness_hover=”100″ filter_contrast_hover=”100″ filter_invert_hover=”0″ filter_sepia_hover=”0″ filter_opacity_hover=”100″ filter_blur_hover=”0″ type=”legacy”][fusion_builder_row][fusion_builder_column type=”1_1″ layout=”1_1″ spacing=”” center_content=”no” link=”” target=”_self” min_height=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” hover_type=”none” border_color=”” border_style=”solid” border_position=”all” box_shadow=”no” box_shadow_blur=”0″ box_shadow_spread=”0″ box_shadow_color=”” box_shadow_style=”” background_type=”single” gradient_start_position=”0″ gradient_end_position=”100″ gradient_type=”linear” radial_direction=”center center” linear_angle=”180″ background_color=”” background_image=”” background_image_id=”” background_position=”left top” background_repeat=”no-repeat” background_blend_mode=”none” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” filter_type=”regular” filter_hue=”0″ filter_saturation=”100″ filter_brightness=”100″ filter_contrast=”100″ filter_invert=”0″ filter_sepia=”0″ filter_opacity=”100″ filter_blur=”0″ filter_hue_hover=”0″ filter_saturation_hover=”100″ filter_brightness_hover=”100″ filter_contrast_hover=”100″ filter_invert_hover=”0″ filter_sepia_hover=”0″ filter_opacity_hover=”100″ filter_blur_hover=”0″ first=”true” last=”true” border_sizes_top=”0″ border_sizes_bottom=”0″ border_sizes_left=”0″ border_sizes_right=”0″][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”default” rule_size=”” rule_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””]

Have you ever heard about the Fibonacci Sequence? It often comes up in interviews, exams, or mathematics quizzes at that time you had questions in your mind like what, why, when, how this Fibonacci is related to us, or in other words why is the Fibonacci so important. Let’s learn it.

[/fusion_text][fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” rotation_text=”” highlight_text=”” after_text=”” content_align=”left” size=”2″ font_size=”” animated_font_size=”” line_height=”” letter_spacing=”” text_color=”” animated_text_color=”” highlight_color=”” style_type=”none” sep_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” fusion_font_variant_title_font=””]

What is the Fibonacci Sequence?

[/fusion_title][fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” rotation_text=”” highlight_text=”” after_text=”” content_align=”left” size=”3″ font_size=”” animated_font_size=”” line_height=”” letter_spacing=”” text_color=”” animated_text_color=”” highlight_color=”” style_type=”none” sep_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” fusion_font_variant_title_font=””]

Who discovered it?

[/fusion_title][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”default” rule_size=”” rule_color=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=””]

Fibonacci is also known as Leonardo BonacciLeonardo of Pisa, or Leonardo Bigollo Pisano (‘Leonardo the Traveller from Pisa’), was an Italian mathematician from the Republic of Pisa, considered to be “the most talented Western mathematician of the Middle Ages”.

In his Young Age, he learned about the Hindu–Arabic numeral system in Bugia, Algeria. Fibonacci traveled around the Mediterranean coast, meeting with many merchants and learning about their systems of doing arithmetic. He soon realized the many advantages of the Hindu-Arabic system, which, unlike the Roman numerals used at the time, allowed easy calculation using a place-value system.

In 1202, he completed the Liber Abaci (Book of Abacus or The Book of Calculation), which popularized Hindu–Arabic numerals in Europe.

[Reference: https://en.wikipedia.org/wiki/Fibonacci]

[/fusion_text][fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” highlight_text=”” after_text=”” content_align=”left” size=”3″ font_size=”” animated_font_size=”” line_height=”” letter_spacing=”” text_color=”” animated_text_color=”” highlight_color=”” style_type=”none” sep_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” fusion_font_variant_title_font=””]

How was the Fibonacci Sequence discovered?

[/fusion_title][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”default” rule_size=”” rule_color=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=””]

Fibonacci Sequences or Fibonacci numbers is the answer to the hypothetical Rabbit Breeding question which was solved by Fibonacci (Leonardo Bigollo Pisano) in a Mathematician Competition in Pisa, Italy in 1225.

Question: Beginning with a male and female rabbit, how many pairs of rabbits could be born in a year? 

Answer: 233 pairs of rabbits at the end of a year.

Explanation :

  • Start: At the start, no rabbits are born, as the initial pair has not had time to become pregnant and give birth (0).
  • The first month: One pair of rabbits is born (1).
  • The second month: Again, one pair of rabbits is born as the new rabbits have not yet matured to bear offspring (1).
  • The third month: Two pairs of rabbits ready to reproduce, and one pair is not, so two pairs of rabbits are born (2).
  • The fourth month: Three pairs of rabbits reproduce and 2 pairs of rabbits are not ready, so three pairs of rabbits are born (3).
  • The fifth month: Five pairs of rabbits reproduce offspring and three are not ready to reproduce, so five pairs of rabbits are born (5).
  • And so on.

[/fusion_text][fusion_tagline_box backgroundcolor=”” shadow=”no” shadowopacity=”0.7″ border=”1″ bordercolor=”” highlightposition=”left” content_alignment=”left” link=”” button=”” linktarget=”_self” modal=”” button_size=”” button_type=”” button_border_radius=”” buttoncolor=”default” title=”” description=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””]

Fibonacci was not the first to know about the sequence. It was known in India hundreds of years before! It was discovered and described as “Virahanka” in Indian mathematic scriptures.

[/fusion_tagline_box][fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” highlight_text=”” after_text=”” content_align=”left” size=”3″ font_size=”” animated_font_size=”” line_height=”” letter_spacing=”” text_color=”” animated_text_color=”” highlight_color=”” style_type=”none” sep_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” fusion_font_variant_title_font=””]

Fibonacci Sequence Theory

[/fusion_title][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”default” rule_size=”” rule_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””]

The Fibonacci sequence is a series of numbers starting from 0 and 1, where every number is the sum of the two numbers preceding it

[/fusion_text][fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” highlight_text=”” after_text=”” content_align=”left” size=”3″ font_size=”” animated_font_size=”” line_height=”” letter_spacing=”” text_color=”” animated_text_color=”” highlight_color=”” style_type=”none” sep_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” fusion_font_variant_title_font=””]

Fibonacci Sequence Long List

[/fusion_title][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”default” rule_size=”” rule_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””]

0,1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, …

[/fusion_text][fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” highlight_text=”” after_text=”” content_align=”left” size=”3″ font_size=”” animated_font_size=”” line_height=”” letter_spacing=”” text_color=”” animated_text_color=”” highlight_color=”” style_type=”none” sep_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” fusion_font_variant_title_font=””]

Mathematical equation of Fibonacci sequence

[/fusion_title][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”default” rule_size=”” rule_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””]

xn = xn-1 + xn-2.

where:

  • xn is term number “n”
  • xn−1 is the previous term (n−1)
  • xn−2 is the term before that (n−2)

[/fusion_text][fusion_table fusion_table_type=”1″ fusion_table_rows=”2″ fusion_table_columns=”8″ animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””]

1/1 2/1 3/2 5/3 8/5 13/8 21/13 34/21
1 2 1.5 1.666 1.6 1.625 1.6153 1.61

[/fusion_table][fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” highlight_text=”” after_text=”” content_align=”left” size=”2″ font_size=”” animated_font_size=”” line_height=”” letter_spacing=”” text_color=”” animated_text_color=”” highlight_color=”” style_type=”none” sep_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” fusion_font_variant_title_font=””]

Importance of the Fibonacci Series in Nature

[/fusion_title][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”default” rule_size=”” rule_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””]

When you divide any Fibonacci number (example: 5), with its preceding number (example: 3), the result is very close to 1.618, which is also called “Phi”. “Phi” is also believed to have been used in ancient Greece to denote the ratio of physical perfection.

[/fusion_text][fusion_table fusion_table_type=”1″ fusion_table_rows=”2″ fusion_table_columns=”11″ animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””]

0 1 1 2 3 5 8 13 21 34
    (0+1) (1+1) (1+2) (2+3) (3+5) (5+8) (8+13) (13+21) (…)

[/fusion_table][fusion_imageframe image_id=”3699|medium” max_width=”” style_type=”” blur=”” stylecolor=”” hover_type=”none” bordersize=”” bordercolor=”” borderradius=”” align=”none” lightbox=”no” gallery_id=”” lightbox_image=”” lightbox_image_id=”” alt=”https://en.wikipedia.org/wiki/Fibonacci_number” link=”https://en.wikipedia.org/wiki/Fibonacci_number” linktarget=”_self” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” filter_hue=”0″ filter_saturation=”100″ filter_brightness=”100″ filter_contrast=”100″ filter_invert=”0″ filter_sepia=”0″ filter_opacity=”100″ filter_blur=”0″ filter_hue_hover=”0″ filter_saturation_hover=”100″ filter_brightness_hover=”100″ filter_contrast_hover=”100″ filter_invert_hover=”0″ filter_sepia_hover=”0″ filter_opacity_hover=”100″ filter_blur_hover=”0″]https://tutorials.eu/wp-content/uploads/2020/07/531px-FibonacciSpiral.svg-300×189.png[/fusion_imageframe][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”default” rule_size=”” rule_color=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=””]

The Fibonacci spiral: an approximation of the golden spiral created by drawing circular arcs connecting the opposite corners of squares in the Fibonacci tiling.

  • Fibonacci sequences are indeed found everywhere in nature/life. They’re useful at modeling the growth of animal populations, plant cell growth, snowflake shape, plant shape, cryptography, and of course computer science.

  • The petals of a flower, the seeds of fruits, rows of seeds on a sunflower, the spirals on a shell develop or add up to the Fibonacci numbers.

[/fusion_text][fusion_table fusion_table_type=”2″ fusion_table_rows=”1″ fusion_table_columns=”0″ animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” /][fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” highlight_text=”” after_text=”” content_align=”left” size=”2″ font_size=”” animated_font_size=”” line_height=”” letter_spacing=”” text_color=”” animated_text_color=”” highlight_color=”” style_type=”none” sep_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” fusion_font_variant_title_font=””]

Use of Fibonacci Sequence in Computer science

[/fusion_title][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”default” rule_size=”” rule_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””]

  • The Fibonacci sequence is a good way to learn how to program a Recursive method

[/fusion_text][fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” highlight_text=”” after_text=”” content_align=”left” size=”3″ font_size=”” animated_font_size=”” line_height=”” letter_spacing=”” text_color=”” animated_text_color=”” highlight_color=”” style_type=”none” sep_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” fusion_font_variant_title_font=””]

Fibonacci heap

[/fusion_title][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”default” rule_size=”” rule_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””]

  • A Fibonacci heap is a specific implementation of the heap data structure that makes use of Fibonacci numbers. Fibonacci heaps are used to implement the priority queue element in Dijkstra’s algorithm, giving the algorithm a very efficient running time. (reference: https://en.wikipedia.org/wiki/Fibonacci_heap)

[/fusion_text][fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” highlight_text=”” after_text=”” content_align=”left” size=”3″ font_size=”” animated_font_size=”” line_height=”” letter_spacing=”” text_color=”” animated_text_color=”” highlight_color=”” style_type=”none” sep_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” fusion_font_variant_title_font=””]

TED Talk about Fibonacci Numbers:

[/fusion_title][fusion_youtube id=”https://www.youtube.com/embed/SjSHVDfXHQ4″ alignment=”” autoplay=”false” api_params=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” css_id=”” /][fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” highlight_text=”” after_text=”” content_align=”left” size=”2″ font_size=”” animated_font_size=”” line_height=”” letter_spacing=”” text_color=”” animated_text_color=”” highlight_color=”” style_type=”none” sep_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” fusion_font_variant_title_font=””]

Fibonacci Sequence and C#

[/fusion_title][fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” rotation_text=”” highlight_text=”” after_text=”” content_align=”left” size=”3″ font_size=”” animated_font_size=”” line_height=”” letter_spacing=”” text_color=”” animated_text_color=”” highlight_color=”” style_type=”none” sep_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” fusion_font_variant_title_font=””]Code Structure[/fusion_title][fusion_syntax_highlighter theme=”” language=”x-php” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]VXNpbmcgU3lzdGVtOwogICAgY2xhc3MgUHJvZ3JhbQogICAgewogICAgICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBNYWluKCkKICAgICAgICB7CiAgICAgICAgICAgIC8vRW50ZXIgaG93IG1hbnkgRmlib25hY2NpIG51bWJlcnMgeW91IHdhbnQgdG8gcHJpbnQKICAgICAgICAgICAgaW50IGZpYm9uYWNjaUxlbmd0aDsKICAgICAgICAgICAgCiAgICAgICAgICAgIC8vSGVyZSBmMSBhbmQgZjIgYXJlIFR3byBGaWJvbmFjY2kgbnVtYmVycyB3aGljaCBhcmUgdXNlZCB0byBmaW5kIHRoZSBuZXh0IEZpYm9uYWNjaSBOdW1iZXIKICAgICAgICAgICAgaW50IGYxID0gMCwgZjIgPSAxLCBmMzsKICAgICAgICAgICAgCiAgICAgICAgICAgIC8vUHJvbXB0IG1lc3NhZ2Ugb24gdGhlIGNvbnNvbGUgYWJvdXQgRmlib25hY2NpIE51bWJlcgogICAgICAgICAgICBDb25zb2xlLldyaXRlKCJFbnRlciBob3cgbWFueSBmaWJvbmFjY2kgbnVtYmVyIHlvdSB3YW50OiIpOwoKICAgICAgICAgICAgLy9SZWFkcyB0aGUgbmV4dCBsaW5lIG9mIGNoYXJhY3RlcnMgZnJvbSB0aGUgc3RhbmRhcmQgaW5wdXQgc3RyZWFtIGFuZCBzdG9yZSBpdCBpbiBhIHZhcmlhYmxlLgogICAgICAgICAgICBmaWJvbmFjY2lMZW5ndGggPSBpbnQuUGFyc2UoQ29uc29sZS5SZWFkTGluZSgpKTsKCiAgICAgICAgICAgIC8vcHJpbnRpbmcgMCBhbmQgMQogICAgICAgICAgICBDb25zb2xlLldyaXRlKGYxICsgIiAiICsgZjIgKyAiICIpOwoKICAgICAgICAgICAgLy9sb29wIHN0YXJ0cyBmcm9tIDIgYmVjYXVzZSAwIGFuZCAxIGFyZSBhbHJlYWR5IHByaW50ZWQKICAgICAgICAgICAgZm9yIChpbnQgaSA9IDI7IGkgPCBmaWJvbmFjY2lMZW5ndGg7ICsraSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgLy9GaWJvbmFjY2kgbG9naWMgaXMgOiBUaGUgQ3VycmVudCBOdW1iZXIgaXMgdGhlIHN1bSBvZiB0aGUgMiBwcmVjZWRpbmcgbnVtYmVycwogICAgICAgICAgICAgICAgZjMgPSBmMSArIGYyOwoKICAgICAgICAgICAgICAgIC8vUHJpbnQgdGhlIG5ld2x5IGNyZWF0ZWQgRmlib25hY2NpIG51bWJlcgogICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZShmMyArICIgIik7CgogICAgICAgICAgICAgICAgLy9Ob3cgc2V0IHRoZSBsYXRlc3QgMiBwcmVjZWRpbmcgbnVtYmVycyB0byBmaW5kIHRoZSBuZXh0IEZpYm9uYWNjaSBOdW1iZXIKICAgICAgICAgICAgICAgIGYxID0gZjI7CiAgICAgICAgICAgICAgICBmMiA9IGYzOwogICAgICAgICAgICB9CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCIiKTsKICAgICAgICB9CiAgICB9[/fusion_syntax_highlighter][fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” highlight_text=”” after_text=”” content_align=”left” size=”3″ font_size=”” animated_font_size=”” line_height=”” letter_spacing=”” text_color=”” animated_text_color=”” highlight_color=”” style_type=”none” sep_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” fusion_font_variant_title_font=””]

Code Explanation

[/fusion_title][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”default” rule_size=”” rule_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””]

So what is happening in the code?

  • First, we create 4 integer variables:
    • f1 stores the first Fibonacci Number
    • f2 stores the second Fibonacci Number
    • f3 stores the next Fibonacci Number(3rd Fibonacci Number)
    • fibonacciLength stores Length of the Fibonacci Sequence in terms of Numbers that we want to print out.
  • Now read a value from the input stream and store it in the fibonacciLength variable.
  • Print the first 2 numbers of the Fibonacci Sequence which is already stored in f1 and f2.
  • Now we use a for loop to find the next Fibonacci number in every iteration from the 3rd number to the Length number provided by the user in the fibonacciLength variable.
  • In the Loop,
    • f3=f1+f2 means that the current Fibonacci number is the sum of the last 2 Fibonacci numbers.
    • print f3 (current Fibonacci number) in the console.
    • Now we want to find the next number (4th Fibonacci Number) so we need the sum of 2nd and 3rd Fibonacci Number so in the next line we write logic like
      • f1=f2;
      • f2=f3;

[/fusion_text][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”default” rule_size=”” rule_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””]

Int32. Parse(String) method is used to convert the string representation of a number to its 32-bit signed integer equivalent.

[/fusion_text][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”default” rule_size=”” rule_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””]

Console.ReadLine() reads the next line of characters from the standard input stream.

[/fusion_text][fusion_text columns=”1″ column_min_width=”” column_spacing=”” rule_style=”default” rule_size=”” rule_color=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=””]

WriteLine() is a method of the Console class which is used to print the standard input, output, and error streams for console applications.

[/fusion_text][fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” rotation_text=”” highlight_text=”” after_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” content_align=”left” size=”3″ font_size=”” animated_font_size=”” fusion_font_family_title_font=”” fusion_font_subset_title_font=”” fusion_font_variant_title_font=”” line_height=”” letter_spacing=”” margin_top=”” margin_bottom=”” margin_top_mobile=”” margin_bottom_mobile=”” text_color=”” animated_text_color=”” highlight_color=”” style_type=”none” sep_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” margin_top_small=”” margin_bottom_small=””]

Code Output

[/fusion_title][fusion_imageframe image_id=”3732|medium” max_width=”512″ style_type=”” blur=”” stylecolor=”” hover_type=”none” bordersize=”” bordercolor=”” borderradius=”” align=”none” lightbox=”no” gallery_id=”” lightbox_image=”” lightbox_image_id=”” alt=”” link=”” linktarget=”_self” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” filter_hue=”0″ filter_saturation=”100″ filter_brightness=”100″ filter_contrast=”100″ filter_invert=”0″ filter_sepia=”0″ filter_opacity=”100″ filter_blur=”0″ filter_hue_hover=”0″ filter_saturation_hover=”100″ filter_brightness_hover=”100″ filter_contrast_hover=”100″ filter_invert_hover=”0″ filter_sepia_hover=”0″ filter_opacity_hover=”100″ filter_blur_hover=”0″]https://tutorials.eu/wp-content/uploads/2020/07/fibonacci_output1-300×72.png[/fusion_imageframe][fusion_title title_type=”text” rotation_effect=”bounceIn” display_time=”1200″ highlight_effect=”circle” loop_animation=”off” highlight_width=”9″ highlight_top_margin=”0″ before_text=”” rotation_text=”” highlight_text=”” after_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” content_align=”left” size=”3″ font_size=”” animated_font_size=”” fusion_font_family_title_font=”” fusion_font_subset_title_font=”” fusion_font_variant_title_font=”” line_height=”” letter_spacing=”” margin_top=”” margin_bottom=”” margin_top_mobile=”” margin_bottom_mobile=”” text_color=”” animated_text_color=”” highlight_color=”” style_type=”none” sep_color=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” margin_top_small=”” margin_bottom_small=””]

Summary

[/fusion_title][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”default” rule_size=”” rule_color=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=””]

We learned about Fibonacci Number, History about Fibonacci Number, and How Fibonacci Number is connected with Nature and how we find n number of Fibonacci in C#.

[/fusion_text][/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]

Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide