Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Shavit Borisov
- // HW
- import java.util.Scanner;
- public class FibonacciSeries {
- public static void main(String[] args)
- {
- Scanner in = new Scanner (System.in);
- int seriesLength;
- int a1 = 0, a2 = 1, sum;
- System.out.print("Enter the length of your Fibonacci series: ");
- seriesLength = in.nextInt();
- if(seriesLength == 1)
- System.out.println(a1);
- else if(seriesLength == 2)
- System.out.println(a1 + " " + a2);
- else
- {
- System.out.printf("%d %d ", a1, a2);
- for(int i = 3; i <= seriesLength; i++)
- {
- sum = a1 + a2;
- System.out.print(sum + " ");
- a1 = a2;
- a2 = sum;
- }
- }
- in.close();
- }
- }
Add Comment
Please, Sign In to add comment