import java.util.Scanner;

public class fibonacci{
    public static void main(String[] args){
        int n=0;
        int fib = n+1;
        Scanner X=new Scanner(;
        System.out.print("Enter # of the term: ");
        n = X.nextInt();
            for (int i=3; i<=n; i++)
                    i = i-1 + i-2;
        System.out.print("Term is: "+n);

I know the algorithm is wrong, but I just can't seem to figure it out correctly.
I'm trying to construct the fibonacci algorithm without recursion and arrays.
If anyone could give me suggestions or pointers, it would be much appreciated.

Recommended Answers

All 4 Replies

n = i-1 + i-2;

u given i instead of n

The 33rd term is what i am looking for. With the correction done, it output's 3, which is incorrect ><

Try this:

import java.util.Scanner;

public class Fibonacci {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(;
		System.out.println("Enter no. of term: ");
		int n = scanner.nextInt();
		int prevNo = 0;
		int no = 1;
		int fibo = 0;
		for(int i = 1; i<=n; i++){
			fibo = prevNo + no;
			prevNo = no;
			no = fibo;
		System.out.println(n + "th term = " + fibo);

Thank you very very much!!

Be a part of the DaniWeb community

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