Java - calculate nth Fibonacci number

  • Context: Comp Sci 
  • Thread starter Thread starter Hercuflea
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around writing a Java program to calculate the nth Fibonacci number using a loop. Participants are focused on the implementation details, particularly how to structure the loop for this calculation.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant seeks guidance on how to set up a loop in Java to calculate the nth Fibonacci number, indicating they are familiar with other parts of the program.
  • Another participant emphasizes the importance of understanding the Fibonacci sequence itself, suggesting that knowing the first few numbers is essential for writing the program.
  • Further contributions reiterate the Fibonacci sequence values and suggest storing calculated Fibonacci numbers in an array for efficiency, proposing a method to check if a number has already been calculated before computing it again.

Areas of Agreement / Disagreement

Participants generally agree on the need to understand the Fibonacci sequence and the utility of using an array to store previously calculated values. However, there is no consensus on the specific implementation details of the loop.

Contextual Notes

Some participants mention the starting indices of the Fibonacci sequence, which may lead to different interpretations of how to implement the loop. There is also a lack of clarity regarding the handling of user input and edge cases in the program.

Hercuflea
Messages
593
Reaction score
49

Homework Statement



Hello everyone, I am trying to write a java program that asks the user to input a number n, and the computer calculates the nth Fibonacci number. I am supposed to use a loop to calculate the problem.

My question is, how do I set up the loop to calculate the nth Fibonacci number? I know how to do all of the program except the loop.

Homework Equations



I want to use the "for" loop.

The Attempt at a Solution



import java.util.Scanner;

public class Assignment2 {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter a number n in order to calculate the nth Fibonacci number: ");
int n = input.nextInt();

for(int i = 0; i;){}

System.out.println("The " + n + "th Fibonacci number is: ");

}
}
 
Physics news on Phys.org
Hercuflea said:

Homework Statement



Hello everyone, I am trying to write a java program that asks the user to input a number n, and the computer calculates the nth Fibonacci number. I am supposed to use a loop to calculate the problem.

My question is, how do I set up the loop to calculate the nth Fibonacci number? I know how to do all of the program except the loop.

Homework Equations



I want to use the "for" loop.

The Attempt at a Solution



import java.util.Scanner;

public class Assignment2 {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter a number n in order to calculate the nth Fibonacci number: ");
int n = input.nextInt();

for(int i = 0; i;){}

System.out.println("The " + n + "th Fibonacci number is: ");

}
}

When you post code, put it inside [noparse]
Code:
[/noparse] tags. This makes your code easier to read, by preserving your indentation (if you used any).

You can't write a program to calculate Fibonacci numbers if you don't know how to calculate them yourself. What are the first few numbers in a Fibonacci sequence?
 
Mark44 said:
When you post code, put it inside [noparse]
Code:
[/noparse] tags. This makes your code easier to read, by preserving your indentation (if you used any).

You can't write a program to calculate Fibonacci numbers if you don't know how to calculate them yourself. What are the first few numbers in a Fibonacci sequence?

a1 = 0, a2 = 1, a3 = 1, a4 = 2, a5 = 3, a6 = 5, ...etc. adding the last two numbers to get the nth number.
 
Hercuflea said:
a1 = 0, a2 = 1, a3 = 1, a4 = 2, a5 = 3, a6 = 5, ...etc. adding the last two numbers to get the nth number.

Usually the indices start at zero, so that F_0=0, F_1=1, F_2=1, F_3=2,etc.

I would suggest that you store all calculated Fibonacci numbers in an array of Integers. To start with, you will need to initialize the array to include F_0 and F_1. Then, to calculate the nth number, you first look in the array to see if it has already been calculated; if yes then simply output that entry in the array, if no, then start with the last 2 entries in the array an calculate and store (in the array) all the remaining numbers up until the nth number.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
Replies
11
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K