Fibbonaci using Java

Assoc. Prof. Leon Abdillah

Fibbonaci using Java

Leon Andretti Abdillah

Fibbonaci adalah barisan bilangan, dimana barisan yang anggota ke-(n)-nya merupakan jumlah antara anggota ke-(n)-1 dan ke-(n)-2, dengan anggota pertamanya adalah bilangan 1.

package Package05;

import java.util.Scanner;

public class WhileFibbonaci {

	public static void main(String[] args) {

		System.out.println("Printing Limited set of Fibonacci Sequence");

		// Initialization
		int Fibbonaci = 1, A = 0, B = 1;

		// Input
		System.out.println("Limit of Fibbonaci to show? ");
		int intLimit = 0;
		intLimit = new Scanner(System.in).nextInt();
		System.out.println("---");
		System.out.println(A);
		while (Fibbonaci < intLimit) {
			System.out.println(Fibbonaci); // Output
			Fibbonaci = A + B;
			A = B;
			B = Fibbonaci;
		} // while		
		System.out.println("---");
	} // main
} // class

Hasilnya:

Printing Limited set of Fibonacci Sequence
Limit of Fibbonaci to show? 
25
---
0
1
1
2
3
5
8
13
21
---

Untuk latihan, Anda diminta untuk mengonversi code tersebut ke struktur do while! Selamat mencoba and GoodLuck!!!

Leave a Reply

Your email address will not be published. Required fields are marked *