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");
}
}
}
Comment if you have any doubts.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");
}
}
}