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,520 points
19 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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

0 votes
1 answer

Using while loop i want to print 10 even numbers

Hello @Nitesh, you are pretty much there. ...READ MORE

answered Nov 22, 2018 in Java by Priyaj
• 56,120 points
585 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
28 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,020 points
92 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
45 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,020 points
432 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 in Java by Kalgi
20 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,520 points
34 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
22 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,320 points
200 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
99 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.