Fibonacci Number Help

Discussion in 'Programming' started by demonlord07, Aug 21, 2013.

  1. #1
    Hey guys i'm having a bit of trouble with my program. I'm positive the code i have is right but it's not showing any data.

    import java.util.Random;
    
    public class Test{
      public static void main(String[] args){
        Random r = new Random();
       
     
        System.out.println("fib1: "+fib1);
        System.out.println("fib2: "+fib2);
    Code (markup):
    Can anyone tell me what the output of this code should be? And why it isn't working for me? :/
     
    demonlord07, Aug 21, 2013 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    You should get an error as an output since fib1 and fib2 are used without being declared. You are also missing the actual Fibonacci method.
     
    ThePHPMaster, Aug 22, 2013 IP
  3. poush

    poush Member

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #3
    The method you write is wrong.
    If you have any number, N =6;
    The output should be
    0 1 1 2 3  5
    Code (markup):
    Method is :
    first = 0;
    second = 1;
    (Assuming first and second are two int variables)
    for(int  i=2;i<N;i++)
    {
        System.out.println(first+"  "+second+"\n");
        third = second + first ; (Assuming third is int variable)
        first = second;
        second = third;
    }
    Code (markup):
     
    poush, Aug 24, 2013 IP