Advertisement
satriafu5710

Menampilkan Bilangan Fibonacci Java

Mar 22nd, 2022
1,949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. package com.tutorial;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner userInput = new Scanner(System.in);
  10.  
  11.         int bilangan, hasil;
  12.         int f1 = 0, f2 = 1;
  13.  
  14.         System.out.println("\n\t Menampilkan Bilangan Fibonacci \n");
  15.  
  16.         System.out.print(" Masukkan sebuah bilangan : ");
  17.         bilangan = userInput.nextInt();
  18.  
  19.         System.out.println("\n Bilangan Fibonacci dari " + bilangan);
  20.         System.out.print("\n = 0, 1");
  21.  
  22.         while (true) {
  23.  
  24.             hasil = f2 + f1;
  25.  
  26.             if (hasil > bilangan) {
  27.  
  28.                 break;
  29.             }
  30.  
  31.             System.out.print(", " + hasil);
  32.  
  33.             f1 = f2;
  34.             f2 = hasil;
  35.         }
  36.  
  37.         System.out.println("\n");
  38.     }
  39.  
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement