0

Guys help me to check whether it is right or wrong??

public class fibonacci { int n1=0,n2=1,n3,i; System.out.println("the number n1 and n2"+n1+n2); void series(int count) {         for(i=2;i<count;++i)             {              n3=n1+n2;              System.out.print("the output "+n3);              n1=n2;              n2=n3;            } } public static void main(String[]args){ fibonacci d=new fibonacci(); d.series(10); } }

23rd Dec 2016, 2:00 PM
tharun
tharun - avatar
1 Respuesta
+ 4
The algorithm looks good but the code won't compile and it is a wrong implementation of OOP. It won't compile because in static main method you can't access non-static n1 and n2 variables. You should declare variables inside the count method and you should write the first two number out there too. In this case you can call the count method multiple times and you will get valid results.
25th Dec 2016, 6:40 AM
Tamás Barta
Tamás Barta - avatar