Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
- import java.util.Scanner;
- public class prog1 {
- public static int fib(int n) {
- if (n <= 2) {
- return 1;
- } else {
- return fib(n - 1) + fib(n - 2);
- }
- }
- public static void programm1(String fileName) throws IOException {
- //Reads numbers from file
- int[] numbrid = new int[4];
- int i = 0;
- File file = new File(fileName+".in");
- Scanner scanner = new Scanner(file);
- while (scanner.hasNextLine()) {
- int line = Integer.parseInt(scanner.nextLine());
- numbrid[i] = line;
- i++;
- }
- scanner.close();
- //prints out numbers
- for (int k = 0; k < numbrid.length; k++) {
- System.out.println(numbrid[k]);
- }
- //Writes the Fibonacci numbers into the file
- File file2 = new File(fileName + ".out");
- PrintWriter writer = new PrintWriter(file2, "UTF-8");
- // if file doesn't exist, then creates it
- if (!file2.exists()) {
- file2.createNewFile();
- }
- for (int n = 0; n < numbrid.length; n++) {
- writer.println(fib(numbrid[n]));
- }
- writer.close();
- System.out.println("done");
- }
- public static void main(String[] args) throws IOException {
- String a = "numbrid";
- programm1(a);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement