Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# calculate Fibonacci sequence

Greenhorn
Posts: 4

Hi,

I am a beginner and I need to write a program that calculates the Fibonacci sequence until a certain maximum.

The code works only I have a fencepost issue with the commas. I need to have a comma after every printed entry

except the last. Does anyone have any ideas as to a solution?

__________________________________

println ("This program lists the Fibonacci sequence.");
int maxValue = readInt ("Max value? ");
int term1 = 0;
int term2 = 1;

while (term1 < maxValue) {
int term3 = term1 + term2;
print (", " + term1 );
term1 = term2;
term2 = term3;

}
}
}

Master Rancher
Posts: 1236
35
A few things here:
• Please post the full code, "print" by itself does not work unless you have imported a package or two.
• Please format your code with the "code" tags in this editor.
• Please provide the output that you are currently receiving.
•
Rancher
Posts: 2767
32

ideas as to a solution?

print the first one without a comma
print all following ones with a leading comma

Desiree Bernard
Greenhorn
Posts: 4
Hello,

1. Please find below the output versus expected - you will see the comma problem.

2. I tried printing one number before the loop with the comma afterwards and this screwed up the calculations.

_____________________________________

test #1:run() #1
expected output:This program lists the Fibonacci sequence.
Max value? 10000
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765
your output:This program lists the Fibonacci sequence.
Max value? 10000
, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765
differences:3c3
< 0,·1,·1,·2,·3,·5,·8,·13,·21,·34,·55,·89,·144,·233,·377,·610,·987,·1597,·2584,·4181,·6765
> ,·0,·1,·1,·2,·3,·5,·8,·13,·21,·34,·55,·89,·144,·233,·377,·610,·987,·1597,·2584,·4181,·6765
result: fail

Rancher
Posts: 2767
32
Please post the code wrapped in code tags.  Also post the program's output.

Greenhorn
Posts: 1

int maxValue = //Value entered by the user;
int term1 = 0;
int term2 = 1;
//Print the first two elements
System.out.print(term1+","+term2);
while(term2 < maxValue){
int term3 =term1 + term2;
//Print the sum prepended by comma.
System.out.print(","+term3);
term1 = term2;
term2 = term3;

}
This code eliminates the issue of the comma appearing after the last elements. As the first two elements of the fibonacci sequence is fixed print them first and prepend a comma before each subsequent element is printed.

Desiree Bernard
Greenhorn
Posts: 4
Thank you!

Sheriff
Posts: 4582
128
mir basheer ali: Thank you for your post, and Welcome to the CodeRanch.

A couple of notes on your post: In the Beginning Java forum, we frown upon posting "complete solutions", that is, posting code that can be copied and pasted into the OP's program.  The reason for this is that we want the OP to come to their own conclusions and discover solutions for themselves, so we give hints and guide them but don't write code for them.

It's okay to post code when the OP has already posted a solution, or if your code is a snippet that illustrates an idea you want to get across.   Your post is close to a complete solution -- it's a bit of a judgement call.

The second thing is that if you UseCodeTags (that's a link), your code will look much better.

 It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad! The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS