Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm currently trying to get the following output for my code that I'll provide later on:

Enter an integer (1-46): 2

The 2nd number in the Fibonacci sequence is: 1

I have to insure the suffice "nd, st, th" is in the output at well. Below is the code I've created so far:

import java.util.*;

public class Somethingpart2 {

	public static void main(String[] args) {

		Scanner kbd = new Scanner(;

		//Variable Declaration 
		int number;
		long Fibnumber;
		Boolean accepted, limit;

		//Beginning of user input for the Fibonacci sequence 
		System.out.print("Enter an integer (1-46): ");
		number = kbd.nextInt();

		Fibnumber = Math.round(Math.pow((1+Math.sqrt(5))/2, number) / Math.sqrt(5)); 
		accepted = number >= 1 && number <= 46;
		limit = number == Fibnumber;

		if (accepted) {
			do {
				Fibnumber = Math.round(Math.pow((1+Math.sqrt(5))/2, number) / Math.sqrt(5));
				if ()

			while (limit);
			System.out.println("Not a valid number.");

Am I going about this the right way? Not sure how to go about the if statement portion, but I believe it's needed. Any advice would be much appreciated. I've only been using Java for a few months now.



What I have tried:

So far I've worked on the outer portion of the application. I need some guidance on the meat of it all.
Updated 13-Oct-16 15:49pm
Richard Deeming 13-Oct-16 12:07pm    
Why have you got a loop in there at all? According to Wikipedia[^], you've already calculated the result in a single line:
Fibnumber = Math.round(Math.pow((1 + Math.sqrt(5)) / 2, number) / Math.sqrt(5));

All you need to do is move that line inside an if (number >= 1 && number <= 46) { ... } block, and output the result.
OneStepForward 13-Oct-16 16:34pm    
Based on modifiers for this assignment:

If the user enters a number above 2, then you will need to compute the vale nth Fibonacci number by using a loop.
Richard Deeming 13-Oct-16 16:50pm    
OK, that means you're being too clever for the assignment! :)

Rather than using the rounding method and powers of the golden ratio, the assignment is expecting you to calculate the sequence using a for loop, adding the previous two numbers together at each step.

It's not as efficient as your current calculation, but I guess it's testing how well you understand loops, rather than how good your maths is. :)
Richard MacCutchan 13-Oct-16 12:08pm    
Fibonacci is a sequence of integers: 1, 1, 2, 3, 5, 8 ... so why do you need those suffixes? Perhaps you could explain exactly what output you are trying to create.
Richard Deeming 13-Oct-16 12:29pm    
I guess the suffix is on the ordinal, not the result.

The 6th number ... rather than ... is 8th.

1 solution

This formula is certainly clever to get the answer in 1 go:
Fibnumber = Math.round(Math.pow((1+Math.sqrt(5))/2, number) / Math.sqrt(5));

but power, square root and division on floating point are all eating a lot of cpu clock cycles, it may prove more economical to compute each value of the sequence because the next value only require an integer addition.

Executing your code step by step may help you to understand what the code is doing and what is missing.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^][^][^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.

Advice: build some functions for each part of the problem, it help to handle changes without breaking other things.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900