A Java program that writes Fibonacci numbers into a text file


import java.math.*;
import java.io.*;


public class FibonacciText {

  public static void main(String[] args) {
   
    try {
      FileOutputStream fout = new FileOutputStream("fibonacci.txt");
      OutputStreamWriter out = new OutputStreamWriter(fout, "8859_1");      
      
      BigInteger low = BigInteger.ZERO;
      BigInteger high = BigInteger.ONE;      
      
      int i = 0;  
      do {
        out.write(low.toString() + "\r\n");
        BigInteger temp = high;
        high = high.add(low);
        low = temp;
        i++;
      }
      while (i <= 25);
      out.write(high.toString() + "\r\n");
     
      out.close();
    }
    catch (IOException e) {
      System.err.println(e);
    }

  }

}


Previous | Next | Top | Cafe con Leche

Copyright 2000 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified February 15, 2000