Infolinks

Breaking

Adsense

Friday, January 22, 2016

JAVA LOOP: Display Numbers in Fibonacci Series Code

Hi there! The following tutorial will introduce you to one of the most common problems encountered in programming subjects. Later on, I will provide set of activities and quizzes for you to practice on to improve your programming skills.

Fibonacci series is just one of the exercises given by most programming instructors. Some other exercises are the following:

  1. Create a simple php calculator
  2. Celsius to Fahrenheit Temperature Converter
  3. Identify if odd or even number
  4. Display numbers using a for loop
  5. Solve for the area of a triangle

and many more. These are just some of the activities that I posted here in my blog. You can visit the links if you want.

Fibonacci series is achieved by adding the first and second value to solve for the third number. And then the 2nd and third to solve for the 4th number and so on and forth. The series starts with 0 and 1 and then 1, 3, 5, 8, 13 and so on.  To be able to achieve this, we need to use to looping statement. In my example, I used the while loop.

The following code will display a Fibonacci series and will stop until the 10th number. Your i there will serve as a counter.

Java Code Fibonacci Series

public class Fibonacci {
public static void main(String[] args) {
int a=0;
int b=1;
int c;
int i=1;
System.out.println(b);
while (i<10)
{
c=a+b;
System.out.println(c);
i++;
a=b;
b=c;
}
}}

Output Fibonacci Series

JAVA LOOP: Display Numbers in Fibonacci Series Code

No comments:

Post a Comment

Adbox