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,810 points
976 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

+4 votes
4 answers

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
• 58,090 points
90,626 views
–1 vote
0 answers

Write a program that uses a while loop to read 10 integer numbers from file

Write a program that uses a while ...READ MORE

Apr 16, 2020 in Java by MIH
• 150 points
1,892 views
0 votes
0 answers

Generate random number between two numbers in JavaScript

Is it possible to use JavaScript to ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
462 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,220 points
983 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,120 points
2,233 views
0 votes
1 answer

Generate UML diagrams from Java code?

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

answered Aug 30, 2018 in Java by samarth295
• 2,220 points
849 views
+1 vote
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,620 points
1,033 views
+1 vote
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,290 points
980 views
+1 vote
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,680 points
3,373 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,220 points
3,176 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP