Java Code for generating fibonacci numbers:
import javax.swing.*;
public class Fibonacci {
public static void main (String[] args) {
String inputSequence = "";
int a = 1, b = 1, sum = 0;
int sequence= 0;
inputSequence = JOptionPane.showInputDialog("Enter number of
sequence you want to see: ");
sequence = Integer.parseInt(inputSequence);
int i = 1;
while(i <= sequence){
System.out.printf("Fib(%d)= %3d\n",i,a);
sum = a + b;
a = b;
b = sum;
i++;
}
System.exit(0);
}
}
import javax.swing.*;
public class Fibonacci {
public static void main (String[] args) {
String inputSequence = "";
int a = 1, b = 1, sum = 0;
int sequence= 0;
inputSequence = JOptionPane.showInputDialog("Enter number of
sequence you want to see: ");
sequence = Integer.parseInt(inputSequence);
int i = 1;
while(i <= sequence){
System.out.printf("Fib(%d)= %3d\n",i,a);
sum = a + b;
a = b;
b = sum;
i++;
}
System.exit(0);
}
}
Your thoughts about this post? Leave a commment now.