Title: JH3 Newton Help and the Fibonacci Problem
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

JH3 Newton Help and the Fibonacci Problem

The problems discussed in the associated video are:

- Newton's Algorithm for square roots - this problem is in JH3
- Fibonacci - this problem is NO LONGER IN JH3 because it was replaced with "Learning Enrichment: How Do I Approach Writing a Program?". However, it is good to understand this problem, because I have 2 subsequent Learning Units based on writing a program that solves Fibonacci:
  - Learning Enrichment: Debugging with Print Statements
  - Eclipse Debugger and the Learning Enrichment: How to use the Eclipse debugger
The description of the problem can be found in: Fibonacci Problem
The solution to the problem can be found in:

import java.util.Scanner;

public class Fibonacci
{
    public static void main(String[] args)
    {
        int n;
        Scanner scan = new Scanner(System.in);


        String continueResponse="y";

        while (continueResponse.charAt(0) == 'y')
        {
            System.out.print("Enter in N: ");
            n = scan.nextInt();

            int oldest = 0;
            int previous = 1;
            int fib = n;
            if (n >= 2)
            {
                for (int i=2; i <= n; i++)
                {
                    fib = oldest+previous;
                    oldest = previous;
                    previous = fib;
                }
            }
            System.out.println("Fibonacci("+n+")="+fib);
            System.out.print("Do you want to continue?: (y/n): ");
            continueResponse = scan.next();
            System.out.println(); // blank line
        }
        System.out.println("Have a good day");
    }
}


Last Modified: August 23, 2015 3:30 AM