Scanner class Methods in Java

Fibonacci Series program in Java

 

Fibonacci Series

Fibonacci series is a mathematical series.In this series, first two numbers are written by user and other numbers are generated using these two numbers. The program adds the first two numbers to generate third number. The the fourth number is calculated by adding second and third number. This process goes on until n numbers.

Lets have a look at Fibonacci series generated using 0 and 1 as first two numbers.

0 , 1, 1 , 2, 3, 5, 8, 13, 21, 34,.............n

In above series , each numbers is combination of previous two numbers except first 0 and 1. Third number 1 is calculated by adding first two numbers, 0 and 1 as 0+1=1, then 1+1=2, 2+1=3 so on.

Program with simple approach

There are several ways two write a program. Each programmer has his own way of writing programs. As I said in my previous post, I will make sure these programs can be easily understandable to beginners also.So, I will write in simple way and with other approaches too.

Write a program to print Fibonacci series using for loop.

Program to print Fabonacci series in java.

In the above program,we 

  1. Declared three integer variable, n is for number up to which we want to print Fibonacci series. The variables num1 and num2 are first two numbers, these numbers will start Fibonacci series. We can call them starting points of series.
  2. Read number n using Scanner input method.
  3. Line 8 of program is used to print first two numbers 0 and 1 on screen. The print() will not print values in new line, as we want to print the series in horizontal order. We use single space in print statement so each number will be separated by space in output.
  4. In line 11 of program, for loop is used to print the values in sequence. We used i as loop variable which is initialized with 1 and text condition checks if 1<=n, so loop will execute till i less than or equal to n.
  5. In for loop, we defined a new variable "next" for next value to be printed in series. The sum of num1 and num2 is assigned to next variable, as we know Fibonacci series is generated by adding previous two numbers.
  6. Line 15 and 16 are used for swapping the values of num1 into num2 and num2's value in next variable.This way each time the previous numbers will get updated in loop. 
  7. Line 17 is print statement to print the value of next number. 
  8. After line 17 loop, main and class are closed by using closing }bracket.
  9. Each time loop executes it updates next number and does the same operations repeatedly.
  10. Analyse the output.

Program using method() 

This program will use java method for printing series. Method is a block of code which can be reused in the program. Methods are very effective way to write program. Program becomes clean and readable by using methods.We will learn about methods in future posts.

Program to print Fibonacci Series using method.

Program to print Fibonacci Series in Java

In this program,
Everything is similar except method and invocation of method.
  1. In Line 17 , a method named fibonacci is defined. It has three arguments or parameters of int type. 
  2. The code in the method body enclosed in opening { and closing } brackets is same as last example we used.
  3. Go to line 13, a object of main class "FibonacciSeries2" is created using new keyword.
  4. In line 14, object "fib" is used to call fibonacci method using dot(.) operator.

Program using Recursion

Recursion 

Recursion is a process in which a method invokes itself.It is a way of executed same code again and again. Recursion is used when we want to use a single method to execute statements continuously.The method instead of using some other code invokes itself repeatedly until the task is completed.

Program to print Fibonacci series using recursion.

Program to print Fibonacci series using recursion.
In the above program,
  1. In main method, we define two variable "n" which specifies how many numbers are to be printed. It is initialized with 10  and 10 numbers will be printed on screen.Another variable is num1 which is starting number to generate and print Fibonacci Series. It is initialized with 0 and 0 is first number to be printed in Fibonacci series. 
  2.  The for loop is used to print each number on screen.The numbers of series are returned by an integer type method named fibonacci that takes num1 as an argument.
  3. The print statement is to print the space and than calls the method fibonacci with 0 as an argument because num1=0, then it prints the value returned by method. The increment statements is used to increment the num1's value each time loop executes.
  4. In line 16, the method fibonacci is defined. This method is defined as static using static keyword.That why we did not created any object to invoke method. Remember, to invoke method without objects you need to define method as static.
  5. static:- The static keyword is used so that method can be invoked from anywhere.The static keyword is very powerful keyword.Static methods cannot be overridden and changed. Static means to be in a constant state and do not change.
  6. This method takes one integer argument.
  7. In the method body, we have used if-else-if statement which is used to define base condition.If num1 is 0 , method will return 0 and if its 1 then 1 will be returned. 
  8. If the num1 is greater than 1 , then if-else-if will not be executed. In that case, method will use return statement in line 27.
  9. In line 27, the method calls itself . If num1 =2, then this return statement will call method fibonacci with argument (num1-1) and adds it to fibonacci(num1-2) which is evaluated as fibonacci(1)+fibonacci(0). This will simply add 1 and 0.So, this return statement will return 0+1=1 as third number of Fibonacci Series.
  10. This process is followed until 10 numbers are printed on screen
Note: In recursion, there must be a base condition and return statement in the method body. In  the above program, if-else-if was base condition,base condition  is used to tell minimum expected value and our minimum expected values are 0 and 1. The return statement used to return value by invoking same method repeatedly. Because fibonacci method is calling itself in its own method body that is why it is recursion.
If you like this post, do comment.
Happy Coding!

Comments

Post a Comment

If you have any doubt, ask here