I am trying to practice JAVA by coding a Fibonacci sequence in which the user can declare the length of the Fibonacci starting from 0 to n. Here is my code:
public class test {
public static void main(String args[]) throws IOException {
BufferedReader pao = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter number: ");
String enter = pao.readLine();
int ent = Integer.parseInt(enter);
int total1 = 0;
int total2 = 1;
int total3 = 0;
for (int a = 0; a < ent; a++) {
if (a <= 1) {
System.out.print(" " + total3);
total3 = total1 + total2;
} else if (a == 2) {
System.out.print(" " + total3);
} else {
total1 = total2;
total2 = total3;
total3 = total1 + total2;
System.out.print(" " + total3);
}
}
}
}
Is there another way to do this in a much more simpler, shorter and "Nicer" way? still without using arrays. Thank you in advance.