sona11

sona11

Fibonacci series using java

I wrote this code to calculate Fibonacci numbers by specifying the size. The results are correct, however the one thing that concerns me is the negative sign for some numbers in the larger range of size input.
here is the code

import java.util.Scanner;

public class FibonacciSeries {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a, c = 0;
        int result = 0;
        int b = 1;

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the Number to display Fibonacci Series");
        a = scan.nextInt();

        System.out.println("Fibonacci Seriers is as follows");


        for (int i = 1; i <= a; i++) {
            System.out.print(" "+result+" ");
            result = c + b;
            b = c;
            c = result;

        }

    }

}

As previously said, this function calculates fibonacci numbers by selecting the size. I read the article about it here according to that findings are right however the one thing that bothers me is the negative sign for some values in the greater range of size input.

1- I couldn’t identify a mistake in the code; how can I remove the negative values from the output?
2- Why are there positive values following certain negative values? 3- The first negative value is -1323752223, followed by a positive number.

Thank you very much.

Most Liked

mindriot

mindriot

The problem you are having is integer overflow. The int allocates enough memory to hold a number within a range (specifically 32 bits giving a range of 2^-31 through 2^31 which is -2147483648 to 2147483647), when you add two numbers together and the result is larger then this than it overflows the memory available. Due to the way in which numbers are represented and added in binary, the effect it has is that it wraps around so 2147483647 + 1 = -2147483648.

You can look into integer overflows, to fully understand what is happening it is best to look at how numbers are represented in binary, twos complement, and how to do addition/subtraction. However for the problem at hand you have two options.

First you can use a long which allocates twice as much memory for the numbers and will give you a much larger range before you run into the same problem, but you will run into the same problem with big enough numbers. This is the easiest option, you should be able to just change “int” to “long” when declaring the variables and you will be good.

Alternatively you can look at something like the BigInteger class which will be able to allocate the right amount of memory to hold larger numbers as required, with some small costs in performance and being more awkward to use in some situations.

Where Next?

Popular Backend topics Top

Kurisu
Hello, Please, let’s say I have a website with user authentication made with Elixir/Phoenix, and now want to add a forum to it (using a ...
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
ohm
Does anybody have good learning resources with regards to going into Event Driven Design, Architecture or Sourcing? I got recommended Er...
New
joshi
Hey everybody! I’m working on the project that includes import of Oracle data to PostgreSQL. That data comes as Oracle export (expdp) fi...
New
TwistingTwists
Hello Folks, I am a novice developer from India. Intending to learn Elixir and web apps (phoenix framework). What are things that I MUS...
New
bsek43
Hello everyone, I’ve started learning Elixir and Phoenix few months ago and while I mostly grasped Elixir’s functional concepts and Phoe...
New
Fl4m3Ph03n1x
Background While playing around with dialyzer, typespecs and currying, I was able to create an example of a false positive in dialyzer. ...
New
Fl4m3Ph03n1x
Background I am trying out polymorphic typing with dialyzer. As an example I am using the famous Option type (aka, Maybe Monad) that is n...
New
Manish_bose
Can anyone help me how to convert a long data to wide data in SQL without using hard coding? Regards Manish
New
pillaiindu
Currently reading the book “Programming Phoenix LiveView”. At the end of the Chapter 1, I’m trying to solve the guess game. If the user ...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
New
Fl4m3Ph03n1x
Background Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
New