Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.math.BigInteger;
- import java.text.DecimalFormat;
- public class BigFibonacci {
- /**
- * Program: BigFibonacci.java
- * Creator: Chris Clarke
- * Created: 06.10.2013
- * Purpose: Calculate the first n terms in Fibonacci series.
- * Notes: Uses BigInteger class.
- */
- private static final int MAX = 270;
- private static BigInteger[] Fib = new BigInteger[MAX+1];
- public static void printFibonacciSeries(int n) {
- DecimalFormat df = new DecimalFormat(",###");
- for (int i=1; i<=n; i++) {
- switch(i) {
- case 1 : case 2 :
- Fib[i] = new BigInteger("1"); break;
- default:
- Fib[i] = Fib[i-2].add(Fib[i-1]);
- }//end switch
- System.out.println( i+": " + df.format( Fib[i]));
- }//end for
- }//end printFibonacci()
- public static void main(String[] args) {
- Scanner keybd = new Scanner(System.in);
- int n=0;
- System.out.print("\nEnter number between 1 and "+MAX+": ");
- do {
- n = keybd.nextInt();
- if (n<1 || n>MAX) {
- System.out.println("Number outside range [1.."+MAX+"]. Try again: ");
- }//end if
- } while (n<1 || n>MAX);
- System.out.println("\nThe first "+n+" numbers in the Fibonacci series are:");
- // print the first n Fibonacci numbers
- printFibonacciSeries(n);
- }//end main()
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement