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.

Popular Backend topics Top

Jsdr3398
I’m trying to create a router where everything is in a collection of routes (similar to how I do my routes in expressjs). But it doesn’t ...
New
Jsdr3398
I’ve been working on and rewriting my messaging platform several times for the past two years. With Discords new rebranding, it has reall...
New
Fl4m3Ph03n1x
Background So, I am playing around with a concept named “NewType” and I am taking inspiration from languages like F# and Scala. My objec...
New
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
harwind
I received this error for a binary search programme in C, despite the fact that it requested for inputs and produced the right output. Th...
/c
New
Sumityadav
Hello all, I am new to learning Java Programming and want to learn java from scratch. I was writing a Java program to get the first and l...
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
pillaiindu
What is the difference between using :references and :belongs_to in the following command? bin/rails generate scaffold LineItem product:...
New
Fl4m3Ph03n1x
Background As I often do, I read books to learn and improve myself. I also enjoy teaching and helping others when I can, so this is somet...
New

Other popular topics Top

brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
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
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New
AstonJ
Biggest jackpot ever apparently! :upside_down_face: I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New

Latest in Questions

View all threads ❯