19 February 2014

Program to print Fibonacci numbers below 'n'

Create a new Class with name Fibonacci:

import java.io.*;
class Fibonacci {
    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter the value n");
        int n = Integer.parseInt(br.readLine());
        int a = 0, b = 1, c = 0;
        if (n > 0) {
            System.out.println("---------------------");
            System.out.println("\t" + a);
            System.out.println("\t" + b);
            do {
                c = a + b;
                if(c<=n)
                System.out.println("\t" + c);
                a = b;
                b = c;
            } while (c <= n);
            System.out.println("---------------------");
        } else {
            System.out.println("enter positive n value");
        }
    }
}



Output:(Click on image to enlarge):
Program-to-print-Fibonacci-numbers-output
Comment if you have any doubts.



Contact Form

Name

Email *

Message *

Smooth Graphics Java

// Arrange Bricks in Game //java brick breaker game code without flickering // added game levels 1,2,3 in the game //java paddle ball ga...