/******************* using recursive method **********************/
/**
* 1.2.1 Java program to calculate the Fibonacci Number using recursion
*/
import java.util.Scanner;
public class Fibo {
public static void main(String args[]){
Scanner scan= new Scanner(System.in); //Scanner object to read from the user
int n;
System.out.println(\"Enter the value of n\");
n=scan.nextInt();
System.out.println(\"The \"+n+\". Fibonacci number is \"+calFibonacci(n+1));// Calling the
recursive method
scan.close();
}
//method to calculate Fibonacci number recursively
public static long calFibonacci(int index){
if (index == 1)
return 0;
if (index == 2)
return 1;
return calFibonacci(index - 1) + calFibonacci(index - 2);
}
}
/** Outputs
* Enter the value of n 25
The 25. Fibonacci number is 75025
*/
/******************************using iterative
method************************************/
/**
* 1.2.2 Java program to calculate the fibonacci number using iterative method
*/
import java.util.Scanner;
public class Fibo2 {
public static void main(String args[]){
Scanner scan= new Scanner(System.in); //Scanner object to read from the user
int n;
System.out.println(\"Enter the value of n\");
n=scan.nextInt();
System.out.println(\"The \"+n+\". Fibonacci number is \"+calFibonacci(n+1)); // Calling the
iterative method to calculate fibonacci number
scan.close();
}
public static long calFibonacci(int index){
int first=0;
int second=1;
for(int i=2;i<=index;i++){
int temp=first;
first=first+second;
second=temp;
}
return first;
}
}
/** Output
*
* Enter the value of n 25
The 25. Fibonacci number is 75025
*
*/
/**************************************************/
The efficient method is the iterative way of calculating the number
Explaination
In recursion method we calculate the the numbers from current index till 0. So for each index we
do calculate the fibonacci number again and again. Hence it takes a lot time to calculate the
answers. Whereas in iterative method we do not rework like recursion. Here we store the
previous two numbers(first and second in above code) and calculate the next one. So its a
efficient way to calculate the number. The time complexity in this will be O(n).
Thanks a lot. Please feel free to ask doubts if you have any. God bless you.
Solution
/******************* using recursive method **********************/
/**
* 1.2.1 Java program to calculate the Fibonacci Number using recursion
*/
import java.util.Scanner;
public class Fibo {
public static void main(String args[]){
Scanner scan= new Scanner(System.in); //Scanner object to read from the user
int n;
System.out.println(\"Enter the value of n\");
n=scan.nextInt();
System.out.println(\"The \"+n+\". Fibonacci number is \"+calFibonacci(n+1));// Calling the
recursive method
scan.close();
}
//method to calculate Fibonacci number recursively
public static long calFibonacci(int index){
if (index == 1)
return 0;
if (index == 2)
return 1;
return calFibonacci(index - 1) + calFibonacci(index - 2);
}
}
/** Outputs
* .
1. /******************* using recursive method **********************/
/**
* 1.2.1 Java program to calculate the Fibonacci Number using recursion
*/
import java.util.Scanner;
public class Fibo {
public static void main(String args[]){
Scanner scan= new Scanner(System.in); //Scanner object to read from the user
int n;
System.out.println("Enter the value of n");
n=scan.nextInt();
System.out.println("The "+n+". Fibonacci number is "+calFibonacci(n+1));// Calling the
recursive method
scan.close();
}
//method to calculate Fibonacci number recursively
public static long calFibonacci(int index){
if (index == 1)
return 0;
if (index == 2)
return 1;
return calFibonacci(index - 1) + calFibonacci(index - 2);
}
}
/** Outputs
* Enter the value of n 25
The 25. Fibonacci number is 75025
*/
2. /******************************using iterative
method************************************/
/**
* 1.2.2 Java program to calculate the fibonacci number using iterative method
*/
import java.util.Scanner;
public class Fibo2 {
public static void main(String args[]){
Scanner scan= new Scanner(System.in); //Scanner object to read from the user
int n;
System.out.println("Enter the value of n");
n=scan.nextInt();
System.out.println("The "+n+". Fibonacci number is "+calFibonacci(n+1)); // Calling the
iterative method to calculate fibonacci number
scan.close();
}
public static long calFibonacci(int index){
int first=0;
int second=1;
for(int i=2;i<=index;i++){
int temp=first;
first=first+second;
second=temp;
}
return first;
}
}
/** Output
*
* Enter the value of n 25
The 25. Fibonacci number is 75025
*
3. */
/**************************************************/
The efficient method is the iterative way of calculating the number
Explaination
In recursion method we calculate the the numbers from current index till 0. So for each index we
do calculate the fibonacci number again and again. Hence it takes a lot time to calculate the
answers. Whereas in iterative method we do not rework like recursion. Here we store the
previous two numbers(first and second in above code) and calculate the next one. So its a
efficient way to calculate the number. The time complexity in this will be O(n).
Thanks a lot. Please feel free to ask doubts if you have any. God bless you.
Solution
/******************* using recursive method **********************/
/**
* 1.2.1 Java program to calculate the Fibonacci Number using recursion
*/
import java.util.Scanner;
public class Fibo {
public static void main(String args[]){
Scanner scan= new Scanner(System.in); //Scanner object to read from the user
int n;
System.out.println("Enter the value of n");
n=scan.nextInt();
System.out.println("The "+n+". Fibonacci number is "+calFibonacci(n+1));// Calling the
recursive method
scan.close();
}
//method to calculate Fibonacci number recursively
public static long calFibonacci(int index){
if (index == 1)
return 0;
if (index == 2)
4. return 1;
return calFibonacci(index - 1) + calFibonacci(index - 2);
}
}
/** Outputs
* Enter the value of n 25
The 25. Fibonacci number is 75025
*/
/******************************using iterative
method************************************/
/**
* 1.2.2 Java program to calculate the fibonacci number using iterative method
*/
import java.util.Scanner;
public class Fibo2 {
public static void main(String args[]){
Scanner scan= new Scanner(System.in); //Scanner object to read from the user
int n;
System.out.println("Enter the value of n");
n=scan.nextInt();
System.out.println("The "+n+". Fibonacci number is "+calFibonacci(n+1)); // Calling the
iterative method to calculate fibonacci number
scan.close();
}
public static long calFibonacci(int index){
int first=0;
int second=1;
for(int i=2;i<=index;i++){
5. int temp=first;
first=first+second;
second=temp;
}
return first;
}
}
/** Output
*
* Enter the value of n 25
The 25. Fibonacci number is 75025
*
*/
/**************************************************/
The efficient method is the iterative way of calculating the number
Explaination
In recursion method we calculate the the numbers from current index till 0. So for each index we
do calculate the fibonacci number again and again. Hence it takes a lot time to calculate the
answers. Whereas in iterative method we do not rework like recursion. Here we store the
previous two numbers(first and second in above code) and calculate the next one. So its a
efficient way to calculate the number. The time complexity in this will be O(n).
Thanks a lot. Please feel free to ask doubts if you have any. God bless you.