Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Membuat Barisan Bilangan Fibonacci
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @Developer : Erik Gunawan
* @Nickname : EXz
* @Phone : 0896 5332 0577
* @EMail : admin@erikgunawan.com
* @Gmail : erik.gunawan44@gmail.com
* @Website : www.erikgunawan.com
* @Facebook : facebook.com/revolutionist.exz
* @Twitter : twitter.com/_EXz
*/
public class BarisanFibonacci {
private static BufferedReader br = null;
public static void main(String[] args) {
System.out.println("==================================");
System.out.println("Program Barisan Bilangan Fibonacci");
System.out.println("==================================");
System.out.println();
boolean cek = true;
while (cek) {
br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Masukkan jumlah suku barisan : ");
int n = Integer.parseInt(br.readLine());
System.out.println();
System.out.println("Barisan " + n + " suku pertama bilangan Fibonacci adalah : ");
int[] arr = new int[n];
arr[0] = 0;
arr[1] = 1;
for (int i=2; i<n; i++) {
arr[i] = arr[i-2] + arr[i-1];
}
for (int i=0; i<arr.length; i++) {
if (i==arr.length-1)
System.out.print(arr[i]);
else
System.out.print(arr[i] + ", ");
}
System.out.println();
System.out.print("Ingin coba lagi (Y/N) ? ");
String coba = br.readLine();
if (coba.equalsIgnoreCase("N"))
cek = false;
else if (coba.equalsIgnoreCase("Y"))
cek = true;
else
System.exit(0);
}
catch (IOException ioe) {
System.out.println("Error IOException");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.