Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Fibonacci {
- private static int fib(int prev_Total, int current_Num){
- return current_Num + prev_Total; // return sum of the two
- }
- public static void main(String[] args){
- int f_Num, s_Num, maxOutput;
- Scanner maxNum = new Scanner(System.in);
- System.out.println("Enter the first value: ");
- f_Num = maxNum.nextInt();
- System.out.println("Enter the second value: ");
- s_Num = maxNum.nextInt();
- System.out.println("Enter the maximum value of the series: ");
- maxOutput = maxNum.nextInt();
- System.out.println(f_Num); // print the first value by default
- System.out.println(s_Num); // print the second value by default
- int prevTotal = f_Num; // initialise prevTotal
- int currentNum = s_Num; // initialise currentTotal
- int output = 0; // initialise output
- while (output < maxOutput){
- output = fib(currentNum, prevTotal); // assign the result of first two numbers added together to first output
- prevTotal = currentNum; // update prevTotal (currentNum becomes our new prevTotal)
- currentNum = output; // update currentNum (output becomes our new currentNum)
- System.out.println(output); // print output
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement