SlideShare a Scribd company logo
1 of 5
Download to read offline
/******************* 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
* 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.

More Related Content

Similar to using recursive method .pdf

Procedure to create_the_calculator_application java
Procedure to create_the_calculator_application javaProcedure to create_the_calculator_application java
Procedure to create_the_calculator_application javagthe
 
Quest 1 define a class batsman with the following specifications
Quest  1 define a class batsman with the following specificationsQuest  1 define a class batsman with the following specifications
Quest 1 define a class batsman with the following specificationsrajkumari873
 
The java program that prompts user to enter a string and .pdf
  The java program that prompts user to  enter a string and .pdf  The java program that prompts user to  enter a string and .pdf
The java program that prompts user to enter a string and .pdfDEEPAKSONI562
 
PrimeRange.java import java.util.Scanner;public class PrimeRan.pdf
PrimeRange.java import java.util.Scanner;public class PrimeRan.pdfPrimeRange.java import java.util.Scanner;public class PrimeRan.pdf
PrimeRange.java import java.util.Scanner;public class PrimeRan.pdfAnkitchhabra28
 
Import java
Import javaImport java
Import javaheni2121
 
java program assigment -1
java program assigment -1java program assigment -1
java program assigment -1Ankit Gupta
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptxKimVeeL
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL fileRACHIT_GUPTA
 
Assume you have a scanner object (called input).Declare an integer.pdf
Assume you have a scanner object (called input).Declare an integer.pdfAssume you have a scanner object (called input).Declare an integer.pdf
Assume you have a scanner object (called input).Declare an integer.pdfezzi552
 
Java Practical File Diploma
Java Practical File DiplomaJava Practical File Diploma
Java Practical File Diplomamustkeem khan
 

Similar to using recursive method .pdf (16)

Procedure to create_the_calculator_application java
Procedure to create_the_calculator_application javaProcedure to create_the_calculator_application java
Procedure to create_the_calculator_application java
 
LAB1.docx
LAB1.docxLAB1.docx
LAB1.docx
 
Quest 1 define a class batsman with the following specifications
Quest  1 define a class batsman with the following specificationsQuest  1 define a class batsman with the following specifications
Quest 1 define a class batsman with the following specifications
 
The java program that prompts user to enter a string and .pdf
  The java program that prompts user to  enter a string and .pdf  The java program that prompts user to  enter a string and .pdf
The java program that prompts user to enter a string and .pdf
 
PrimeRange.java import java.util.Scanner;public class PrimeRan.pdf
PrimeRange.java import java.util.Scanner;public class PrimeRan.pdfPrimeRange.java import java.util.Scanner;public class PrimeRan.pdf
PrimeRange.java import java.util.Scanner;public class PrimeRan.pdf
 
Import java
Import javaImport java
Import java
 
Programs.pptx
Programs.pptxPrograms.pptx
Programs.pptx
 
java program assigment -1
java program assigment -1java program assigment -1
java program assigment -1
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL file
 
Assume you have a scanner object (called input).Declare an integer.pdf
Assume you have a scanner object (called input).Declare an integer.pdfAssume you have a scanner object (called input).Declare an integer.pdf
Assume you have a scanner object (called input).Declare an integer.pdf
 
Java Practical File Diploma
Java Practical File DiplomaJava Practical File Diploma
Java Practical File Diploma
 
programming for Calculator in java
programming for Calculator in javaprogramming for Calculator in java
programming for Calculator in java
 
07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt
 
Java final lab
Java final labJava final lab
Java final lab
 
Java doc Pr ITM2
Java doc Pr ITM2Java doc Pr ITM2
Java doc Pr ITM2
 

More from vichu19891

1. Copper is above silver in the activity series. Thus Cu metal will.pdf
1. Copper is above silver in the activity series. Thus Cu metal will.pdf1. Copper is above silver in the activity series. Thus Cu metal will.pdf
1. Copper is above silver in the activity series. Thus Cu metal will.pdfvichu19891
 
a. Population - families in the state of Florida b. Variable .pdf
 a. Population - families in the state of Florida b. Variable .pdf a. Population - families in the state of Florida b. Variable .pdf
a. Population - families in the state of Florida b. Variable .pdfvichu19891
 
there is no reaction between HNO3 and KCl. S.pdf
                     there is no reaction between HNO3 and KCl.  S.pdf                     there is no reaction between HNO3 and KCl.  S.pdf
there is no reaction between HNO3 and KCl. S.pdfvichu19891
 
The thickness of non-saturated zone and physico-c.pdf
                     The thickness of non-saturated zone and physico-c.pdf                     The thickness of non-saturated zone and physico-c.pdf
The thickness of non-saturated zone and physico-c.pdfvichu19891
 
The folding process of proteins is hierarchical, .pdf
                     The folding process of proteins is hierarchical, .pdf                     The folding process of proteins is hierarchical, .pdf
The folding process of proteins is hierarchical, .pdfvichu19891
 
Step1 ppt of PbCl2 are soluble in hot water. Ste.pdf
                     Step1 ppt of PbCl2 are soluble in hot water.  Ste.pdf                     Step1 ppt of PbCl2 are soluble in hot water.  Ste.pdf
Step1 ppt of PbCl2 are soluble in hot water. Ste.pdfvichu19891
 
Should take off H from SH group, forming RS-Na+ s.pdf
                     Should take off H from SH group, forming RS-Na+ s.pdf                     Should take off H from SH group, forming RS-Na+ s.pdf
Should take off H from SH group, forming RS-Na+ s.pdfvichu19891
 
Rest are okay but B) I think should be vanderwall.pdf
                     Rest are okay but B) I think should be vanderwall.pdf                     Rest are okay but B) I think should be vanderwall.pdf
Rest are okay but B) I think should be vanderwall.pdfvichu19891
 
pH =4.217 pH = - log(concentration of H+) = -log .pdf
                     pH =4.217 pH = - log(concentration of H+) = -log .pdf                     pH =4.217 pH = - log(concentration of H+) = -log .pdf
pH =4.217 pH = - log(concentration of H+) = -log .pdfvichu19891
 
Liquids may change to a vapor at temperatures bel.pdf
                     Liquids may change to a vapor at temperatures bel.pdf                     Liquids may change to a vapor at temperatures bel.pdf
Liquids may change to a vapor at temperatures bel.pdfvichu19891
 
intra extra equilibrium potential mEqL mEqL 1.pdf
                     intra extra equilibrium potential  mEqL mEqL  1.pdf                     intra extra equilibrium potential  mEqL mEqL  1.pdf
intra extra equilibrium potential mEqL mEqL 1.pdfvichu19891
 
Which of the following forms of DES is considered the most vulnerabl.pdf
Which of the following forms of DES is considered the most vulnerabl.pdfWhich of the following forms of DES is considered the most vulnerabl.pdf
Which of the following forms of DES is considered the most vulnerabl.pdfvichu19891
 
This is actually pretty simple, so Ill help explain. Take a gi.pdf
This is actually pretty simple, so Ill help explain. Take a gi.pdfThis is actually pretty simple, so Ill help explain. Take a gi.pdf
This is actually pretty simple, so Ill help explain. Take a gi.pdfvichu19891
 
There are many test IPv6 networks deployed across the world. For act.pdf
There are many test IPv6 networks deployed across the world. For act.pdfThere are many test IPv6 networks deployed across the world. For act.pdf
There are many test IPv6 networks deployed across the world. For act.pdfvichu19891
 
The three ways of presenting the changes in the balance of the Compr.pdf
The three ways of presenting the changes in the balance of the Compr.pdfThe three ways of presenting the changes in the balance of the Compr.pdf
The three ways of presenting the changes in the balance of the Compr.pdfvichu19891
 
The RASopathies are a group of genetic syndromes caused by germline .pdf
The RASopathies are a group of genetic syndromes caused by germline .pdfThe RASopathies are a group of genetic syndromes caused by germline .pdf
The RASopathies are a group of genetic syndromes caused by germline .pdfvichu19891
 
ThanksSolutionThanks.pdf
ThanksSolutionThanks.pdfThanksSolutionThanks.pdf
ThanksSolutionThanks.pdfvichu19891
 
Step1 In O2 ; we have 2 unpaired electrons which occupy pi 2p Anti.pdf
Step1 In O2 ; we have 2 unpaired electrons which occupy pi 2p  Anti.pdfStep1 In O2 ; we have 2 unpaired electrons which occupy pi 2p  Anti.pdf
Step1 In O2 ; we have 2 unpaired electrons which occupy pi 2p Anti.pdfvichu19891
 
Quartzite is sandstone that has been converted to a solid quartz roc.pdf
Quartzite is sandstone that has been converted to a solid quartz roc.pdfQuartzite is sandstone that has been converted to a solid quartz roc.pdf
Quartzite is sandstone that has been converted to a solid quartz roc.pdfvichu19891
 
Principal amount = $800,0001.066^(512)Interest in the last y.pdf
Principal amount = $800,0001.066^(512)Interest in the last y.pdfPrincipal amount = $800,0001.066^(512)Interest in the last y.pdf
Principal amount = $800,0001.066^(512)Interest in the last y.pdfvichu19891
 

More from vichu19891 (20)

1. Copper is above silver in the activity series. Thus Cu metal will.pdf
1. Copper is above silver in the activity series. Thus Cu metal will.pdf1. Copper is above silver in the activity series. Thus Cu metal will.pdf
1. Copper is above silver in the activity series. Thus Cu metal will.pdf
 
a. Population - families in the state of Florida b. Variable .pdf
 a. Population - families in the state of Florida b. Variable .pdf a. Population - families in the state of Florida b. Variable .pdf
a. Population - families in the state of Florida b. Variable .pdf
 
there is no reaction between HNO3 and KCl. S.pdf
                     there is no reaction between HNO3 and KCl.  S.pdf                     there is no reaction between HNO3 and KCl.  S.pdf
there is no reaction between HNO3 and KCl. S.pdf
 
The thickness of non-saturated zone and physico-c.pdf
                     The thickness of non-saturated zone and physico-c.pdf                     The thickness of non-saturated zone and physico-c.pdf
The thickness of non-saturated zone and physico-c.pdf
 
The folding process of proteins is hierarchical, .pdf
                     The folding process of proteins is hierarchical, .pdf                     The folding process of proteins is hierarchical, .pdf
The folding process of proteins is hierarchical, .pdf
 
Step1 ppt of PbCl2 are soluble in hot water. Ste.pdf
                     Step1 ppt of PbCl2 are soluble in hot water.  Ste.pdf                     Step1 ppt of PbCl2 are soluble in hot water.  Ste.pdf
Step1 ppt of PbCl2 are soluble in hot water. Ste.pdf
 
Should take off H from SH group, forming RS-Na+ s.pdf
                     Should take off H from SH group, forming RS-Na+ s.pdf                     Should take off H from SH group, forming RS-Na+ s.pdf
Should take off H from SH group, forming RS-Na+ s.pdf
 
Rest are okay but B) I think should be vanderwall.pdf
                     Rest are okay but B) I think should be vanderwall.pdf                     Rest are okay but B) I think should be vanderwall.pdf
Rest are okay but B) I think should be vanderwall.pdf
 
pH =4.217 pH = - log(concentration of H+) = -log .pdf
                     pH =4.217 pH = - log(concentration of H+) = -log .pdf                     pH =4.217 pH = - log(concentration of H+) = -log .pdf
pH =4.217 pH = - log(concentration of H+) = -log .pdf
 
Liquids may change to a vapor at temperatures bel.pdf
                     Liquids may change to a vapor at temperatures bel.pdf                     Liquids may change to a vapor at temperatures bel.pdf
Liquids may change to a vapor at temperatures bel.pdf
 
intra extra equilibrium potential mEqL mEqL 1.pdf
                     intra extra equilibrium potential  mEqL mEqL  1.pdf                     intra extra equilibrium potential  mEqL mEqL  1.pdf
intra extra equilibrium potential mEqL mEqL 1.pdf
 
Which of the following forms of DES is considered the most vulnerabl.pdf
Which of the following forms of DES is considered the most vulnerabl.pdfWhich of the following forms of DES is considered the most vulnerabl.pdf
Which of the following forms of DES is considered the most vulnerabl.pdf
 
This is actually pretty simple, so Ill help explain. Take a gi.pdf
This is actually pretty simple, so Ill help explain. Take a gi.pdfThis is actually pretty simple, so Ill help explain. Take a gi.pdf
This is actually pretty simple, so Ill help explain. Take a gi.pdf
 
There are many test IPv6 networks deployed across the world. For act.pdf
There are many test IPv6 networks deployed across the world. For act.pdfThere are many test IPv6 networks deployed across the world. For act.pdf
There are many test IPv6 networks deployed across the world. For act.pdf
 
The three ways of presenting the changes in the balance of the Compr.pdf
The three ways of presenting the changes in the balance of the Compr.pdfThe three ways of presenting the changes in the balance of the Compr.pdf
The three ways of presenting the changes in the balance of the Compr.pdf
 
The RASopathies are a group of genetic syndromes caused by germline .pdf
The RASopathies are a group of genetic syndromes caused by germline .pdfThe RASopathies are a group of genetic syndromes caused by germline .pdf
The RASopathies are a group of genetic syndromes caused by germline .pdf
 
ThanksSolutionThanks.pdf
ThanksSolutionThanks.pdfThanksSolutionThanks.pdf
ThanksSolutionThanks.pdf
 
Step1 In O2 ; we have 2 unpaired electrons which occupy pi 2p Anti.pdf
Step1 In O2 ; we have 2 unpaired electrons which occupy pi 2p  Anti.pdfStep1 In O2 ; we have 2 unpaired electrons which occupy pi 2p  Anti.pdf
Step1 In O2 ; we have 2 unpaired electrons which occupy pi 2p Anti.pdf
 
Quartzite is sandstone that has been converted to a solid quartz roc.pdf
Quartzite is sandstone that has been converted to a solid quartz roc.pdfQuartzite is sandstone that has been converted to a solid quartz roc.pdf
Quartzite is sandstone that has been converted to a solid quartz roc.pdf
 
Principal amount = $800,0001.066^(512)Interest in the last y.pdf
Principal amount = $800,0001.066^(512)Interest in the last y.pdfPrincipal amount = $800,0001.066^(512)Interest in the last y.pdf
Principal amount = $800,0001.066^(512)Interest in the last y.pdf
 

Recently uploaded

Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17Celine George
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppCeline George
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportDenish Jangid
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxCeline George
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint23600690
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxLimon Prince
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 

Recently uploaded (20)

Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 

using recursive method .pdf

  • 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.