I am trying to make a Fibonacci sequence program to display up to the nth term(only positive numbers).

For some reason it doesn't seem to work.

import java.util.Scanner;

/**
 *
 * @author Toby
 */
public class Fabonnacci {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Enter the trem of the fabonacci sequence:  ");
        Scanner input = new Scanner(System.in);
        int term = input.nextInt();
        int x =0;
        int y =0;
        
        int output =0;
        int first3[] = new int[4];
        first3[0]=0;
        first3[1]=1;
        first3[2]=1;
        first3[3]=2;
        
        for(int i = 0;i<=term;i++){
            
            if(term<=3){
                output=first3[term];
                System.out.print(first3[term]);
                
            }else{
                
               
                if(i==0){
                     System.out.print("0,1,1,2");
                    x=1;
                    y=2;
                    System.out.print("," + (x+y));
                }else if(x>y){
                    y+=x;
                    System.out.print(","+ y);
                    
                }else if(y>x){
                    x+=y;
                    System.out.print(","+ y);
                }
                    
                }
            
                
            
}
        
        
        }
    }

NOTE - this is not a school project. I am teaching myself java.

Recommended Answers

All 3 Replies

"it doesn't seem to work" gives us nothing to go on. Exactly what error messages do you get, or exactly how does the output differ from the expected output? Be as precise as possible.

NOTE - this is not a school project. I am teaching myself java.

So why dont you try to google it, I know at least 5 open source projects that have Fibonacci as part of them

Googling open source code is certainly a good way to find algorithms, examples, and techniques, but IMHO when you're learning there's no substitute for starting with a blank page and writing, testing, and debugging some code of your own.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.