Q. Write a java program to generate the Fibonacci sequence up to a give number.The program should be prompt the user to enter a positive integer. Display the sequence.

Fibonacci-example-1

				
					class fibno
{
	public static void main(String arg[])
	{
		int n1=0,n2=1,n3,count=10;
		System.out.print(n1 +" "+n2);
		for(int i=2;i<=count;i++)
		{
		n3=n1+n2;
		n1=n2;
		n2=n3;
		System.out.print(" "+n3);

		}
	}

}
				
			

Out Put

fibonacci-example-1

Fibonacci-example-2

				
					class FibonacciExample1{
public static void main(String args[])
{
 int n1=0,n2=1,n3,i,count=10;
 System.out.print(n1+" "+n2);//printing 0 and 1

 for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
 {
  n3=n1+n2;
  System.out.print(" "+n3);
  n1=n2;
  n2=n3;
 }

}}
				
			

Out Put

Fibonacci-example-3

				
					class FibonacciExample2{  
 static int n1=0,n2=1,n3=0;    
 static void printFibonacci(int count){    
    if(count>0){    
         n3 = n1 + n2;    
         n1 = n2;    
         n2 = n3;    
         System.out.print(" "+n3);   
         printFibonacci(count-1);    
     }    
 }    
 public static void main(String args[]){    
  int count=10;    
  System.out.print(n1+" "+n2);//printing 0 and 1    
  printFibonacci(count-2);//n-2 because 2 numbers are already printed   
 }  
}  
				
			

Out Put

The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones. It is named after Leonardo of Pisa, who was known as Fibonacci. The sequence starts with 0 and 1, and each subsequent number is the sum of the two preceding numbers. Mathematically, the Fibonacci sequence is defined by the recurrence relation:

       F(n)=F(n1)+F(n2)

where represents the th Fibonacci number, and and are the initial terms of the sequence.

Here’s a deeper look into the Fibonacci sequence

  1. Definition: The Fibonacci sequence starts with 0 and 1, and the subsequent numbers are generated by adding the two previous numbers. So, the sequence begins: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on.

  2. Properties:

    1. Each number in the Fibonacci sequence is called a Fibonacci number.
    2. The ratio of consecutive Fibonacci numbers tends to approach the golden ratio (≈1.61803), as increases.
    3. Fibonacci numbers appear in many natural phenomena, such as the branching in trees, the arrangement of leaves on a stem, the flowering of artichokes, and the structure of pinecones and sunflowers.
    4. Formula: While the recursive formula is commonly used to generate Fibonacci numbers, there is also a closed-form expression to directly compute the th Fibonacci number: 

F(n) = 5ϕn(1ϕ)nwhere is the golden ratio (1+52).

5.Applications: Fibonacci numbers and the Fibonacci sequence appear in various areas, including mathematics, computer science, art, architecture, and nature. They are used in algorithms, especially in dynamic programming and optimization problems. Additionally, Fibonacci numbers have applications in number theory, cryptography, and computational biology.

The Fibonacci sequence is a fascinating mathematical concept that demonstrates the interconnectedness of mathematics with nature and art. Its simple recursive definition leads to a rich array of properties and applications across different disciplines.

 
 
Scroll to Top