Row Of Fibonacci

Java Code

/**
 * RowOfFibonacci.java
 * Created by Stijn Strickx on Oct. 30 2005
 * Copyright 2005 Stijn Strickx, All rights reserved
 */

import java.util.*;
import java.io.*;
import java.math.BigInteger;

public class RowOfFibonacci {
public static void main(String[] arguments) throws java.io.IOException {


	BigInteger Fibonacci1 = new BigInteger("1");
	BigInteger Fibonacci2 = new BigInteger("1");
	BigInteger Fibonacci3 = new BigInteger("2");
	long AmountFibonacciNumbers = 0;
	int BreakTeller = 0;

		System.out.println(1);
	
			try {	
			FileOutputStream fos = new FileOutputStream("FibonacciFile.txt");
			PrintStream ps = new PrintStream(fos);
			ps.println(1);

			AmountFibonacciNumbers = getNumber("How many numbers of fibonacci would you like to write to a file? ");

		
		for(int x = 0; x <= AmountFibonacciNumbers-2; x++){
			
			Fibonacci3 = Fibonacci1.add(Fibonacci2);
			Fibonacci1 = Fibonacci2;
			Fibonacci2 = Fibonacci3;
				
				System.out.println(Fibonacci1);
				ps.println(Fibonacci1);
				BreakTeller++;

					if(BreakTeller == 10){
						ps.println();
						BreakTeller = 0;
					}

		}
			System.out.print("You can now access the FibonacciFile.txt which is ");
			System.out.println("in the same directory as the .class file of this program.");
			ps.close();
			} catch (FileNotFoundException e)
			{ System.err.println("error writing to file" + e);
			}

	}
		static long getNumber(String question) throws java.io.IOException {
		String theNumber;
		long number = 0;
		BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
		System.out.print(question);
		theNumber = in.readLine();
		System.out.println();
		number = Long.parseLong(theNumber);
		return number;
	}
}

Home | Code | Learn
© 2007-2008 ProgLogic, all rights reserved. | ProgLogic.com is created by Stijn Strickx. | e-mail