Generate 10 fibonacci numbers.

0 votes

I am using this code and am not getting the desired output:

public class Fibonacci
{
    public static void main(String[] args)
    {
        int a=0,b=1,c=0;
        System.out.print(a+" ");
        for(int i=1;i<10;i++)
        {
            c = a+b;
            b = c;
            a = b;
            System.out.print(" "+c);
        }
    }
}
Output:
0  1 2 4 8 16 32 64 128 256 
Nov 21, 2018 in Java by Jino
• 5,630 points
62 views

1 answer to this question.

0 votes

The placement of logic is wrong that is why you are getting the error:

You should be using :

a = b;
b = c;
c = a + b;

public class Fibonacci
{
    public static void main(String[] args)
    {
        int a=0,b=1,c=0;
        System.out.print(a+" ");
        for(int i=1;i<10;i++)
        {
            a = b;
            b = c;
            c = a + b;
            System.out.print(" "+c);
        }
    }
}
Output:
0  1 1 2 3 5 8 13 21 34
answered Nov 21, 2018 by Nabarupa

Related Questions In Java

+2 votes
2 answers

Using while loop i want to print 10 even numbers

public class Loops { ...READ MORE

answered Dec 4, 2019 in Java by One of the Simplest ways to answer you q
6,833 views
+1 vote
2 answers

How to generate random integers within specific range in Java?

You can achieve that concisely in Java: Random ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
82 views
0 votes
2 answers

Generate an alpha-numeric string randomly

Java supplies a way of doing this ...READ MORE

answered Jul 18, 2018 in Java by Daisy
• 8,080 points
507 views
0 votes
1 answer

Generate UML diagrams from Java code?

You could also give the netbeans UML ...READ MORE

answered Aug 29, 2018 in Java by samarth295
• 2,190 points
166 views
0 votes
1 answer

Where is JAVA_HOME on macOS Mojave (10.14) to Lion (10.7)?

export JAVA_HOME="$(/usr/libexec/java_home -v 1.6)" or export JAVA_HOME="$(/usr/libexec/java_home -v 1.7)" or export ...READ MORE

answered Dec 4, 2018 in Java by Daisy
• 8,080 points
1,201 views
0 votes
1 answer

How to install and configure JDK8 on Windows 10

Hey @Henna, Go to this page  Accept the license ...READ MORE

answered Feb 28, 2019 in Java by Kalgi
138 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,560 points
89 views
0 votes
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,280 points
84 views
0 votes
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,660 points
632 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
982 views