9:05 pm - Thursday October 18, 2018

Java Program for Fibonacci numbers

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

/**

*@author:CandidJava.com

*@description :This program find the Fibonacci numbers in ‘n’ numbers

*/

import java.io.*;

class Fibonacci{

public static void main(String args[]) {

System.out.println("How many numbers of the sequence would you like?");

InputStreamReader sr = new InputStreamReader(System.in);

BufferedReader br    = new BufferedReader(sr);

try {

String input = br.readLine();

int n = Integer.valueOf(input).intValue();

fibonacci(n);

}

catch (NumberFormatException e){

System.out.println("That is not an integer. Please enter an integer value");

}

catch (IOException e) {

System.out.println("I did not recieve an input");

}

}

public static void fibonacci(int n){

int a=0,b=1;

for (int i=0;i<n;i++){

System.out.println(a);

a=a+b;

b=a-b;

}

}

}

Output

D:\ Candidjava>Code>javac Fibonacci1.java

D:\ Candidjava>Code> java Fibonacci1

How many numbers of the sequence would you like?

10

0

1

1

2

3

5

8

13

21

34

Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply