Fibonacci program using only two variables


#1

is it possible to write fibonacci program using only two variables


#2

yes it is possible to write fibonacci program by using only two variables. you just need two variable to keep track of values. one intermediate variable is used for running loops.
here is program and compiled output with it.


#3

yes it is possible,

class Fibonacci
{
public static void main (String[] args)
{
//We declare 2 variables.
int value1 = 0,value2 = 0;

	value2++;//Value2 variable increments by one.
	//We want to show only 10 values.
	for(int i=0;i<10;i++){
	    //We print the data on each variable.
	    System.out.println(value1);
	    System.out.println(value2);
	    //Finally we proceed to do the add operation.
	    value1=value1+value2;
	    value2=value1+value2;
	}
}

}