Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Fibonacci {
  4.  
  5. private static int fib(int prev_Total, int current_Num){
  6.  
  7. return current_Num + prev_Total; // return sum of the two
  8. }
  9.  
  10.  
  11. public static void main(String[] args){
  12. int f_Num, s_Num, maxOutput;
  13.  
  14. Scanner maxNum = new Scanner(System.in);
  15.  
  16. System.out.println("Enter the first value: ");
  17. f_Num = maxNum.nextInt();
  18. System.out.println("Enter the second value: ");
  19. s_Num = maxNum.nextInt();
  20. System.out.println("Enter the maximum value of the series: ");
  21. maxOutput = maxNum.nextInt();
  22.  
  23. System.out.println(f_Num); // print the first value by default
  24. System.out.println(s_Num); // print the second value by default
  25.  
  26. int prevTotal = f_Num; // initialise prevTotal
  27. int currentNum = s_Num; // initialise currentTotal
  28. int output = 0; // initialise output
  29.  
  30. while (output < maxOutput){
  31.  
  32. output = fib(currentNum, prevTotal); // assign the result of first two numbers added together to first output
  33. prevTotal = currentNum; // update prevTotal (currentNum becomes our new prevTotal)
  34. currentNum = output; // update currentNum (output becomes our new currentNum)
  35. System.out.println(output); // print output
  36.  
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement