{"id":41,"date":"2016-01-09T15:26:51","date_gmt":"2016-01-09T15:26:51","guid":{"rendered":"http:\/\/muthu.co\/?p=41"},"modified":"2021-01-02T14:06:16","modified_gmt":"2021-01-02T14:06:16","slug":"many-ways-of-writing-the-fibonacci-series-program-using-java","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/many-ways-of-writing-the-fibonacci-series-program-using-java\/","title":{"rendered":"Many ways of writing the Fibonacci series program using Java"},"content":{"rendered":"
This is supposed to be a super simple program but how many ways can you write it?
\nHere are a few.<\/p>\n
Program 1 \u00a0– Fibonacci with 2 temp variables<\/strong><\/p>\n Program 2 – Fibonacci with 3 temp variables<\/strong><\/p>\n Program 3 – Fibonacci using recursion<\/strong><\/p>\n <\/p>\npublic class Fibonacci {\r\n\u00a0 \u00a0 \u00a0 \u00a0 public static void main(String[] args) { \r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 int n = 10;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 int f = 0, g = 1;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0for (int i = 1; i <= n; i++) {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0f = f + g;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0g = f - g;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0System.out.println(f); \r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0}\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0}\r\n}<\/pre>\n
public class Fibonacci { \r\n\u00a0 \u00a0 \u00a0 \u00a0 public static void main(String args[])\r\n\u00a0 \u00a0 \u00a0 \u00a0 { \r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 int n = 10;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 int f1, f2=0, f3=1; \r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 for(int i=1;i<=num;i++)\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 { \r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0System.out.print(\" \"+f3+\" \"); \r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0f1 = f2; \r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0f2 = f3; \r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0f3 = f1 + f2; \r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0}\r\n}<\/pre>\n
public class Fibonacci\r\n{\r\n\u00a0 \u00a0 \u00a0 \u00a0 public static void main(String[] args)\r\n\u00a0 \u00a0 \u00a0 \u00a0 {\u00a0\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 for(int i=0;i<10;i++)\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 System.out.println(fibonacci(i));\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\r\n\r\n\u00a0 \u00a0 \u00a0 \u00a0 }\r\n\u00a0 \u00a0 \u00a0 \u00a0 public static int fibonacci(int n) {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if(n == 0)\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return 0;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 else if(n == 1)\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return 1;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 else\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return fibonacci(n - 1) + fibonacci(n - 2);\r\n\u00a0 \u00a0 \u00a0 \u00a0 }\r\n}<\/pre>\n