Created
April 26, 2015 14:38
-
-
Save agungsijawir/f06b726a92defa0a76a8 to your computer and use it in GitHub Desktop.
Fibonacci using do-while
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class FiboDoWhile { | |
public static void main(String []args) | |
{ | |
Scanner keyboard = new Scanner(System.in); | |
System.out.print("Input n (integer): "); | |
int n = keyboard.nextInt(); | |
int n_2 = 0, n_1 = 1, a = 0; | |
int fibSeq = 0; | |
if (n == 0) System.out.print(n_2 + " "); | |
else if (n == 1) System.out.print(n_2 + " " + n_1 + " "); | |
else { | |
System.out.print(n_2 + " "); | |
do { | |
fibSeq = n_1 + n_2; | |
n_1 = n_2; | |
n_2 = fibSeq; | |
System.out.print(fibSeq+" "); | |
a++; | |
} while(a<n); | |
} | |
// output: 0 1 1 2 3 5 .... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment