Shavit

Ex. Bank #6

Nov 24th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. // Shavit Borisov
  2. // HW
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class FibonacciSeries {
  7.  
  8.     public static void main(String[] args)
  9.    
  10.     {
  11.         Scanner in = new Scanner (System.in);
  12.        
  13.         int seriesLength;
  14.         int a1 = 0, a2 = 1, sum;
  15.        
  16.         System.out.print("Enter the length of your Fibonacci series: ");
  17.         seriesLength = in.nextInt();
  18.        
  19.         if(seriesLength == 1)
  20.             System.out.println(a1);
  21.         else if(seriesLength == 2)
  22.             System.out.println(a1 + " " + a2);
  23.         else
  24.         {
  25.             System.out.printf("%d %d ", a1, a2);
  26.            
  27.             for(int i = 3; i <= seriesLength; i++)
  28.             {
  29.                 sum = a1 + a2;
  30.                 System.out.print(sum + " ");
  31.                 a1 = a2;
  32.                 a2 = sum;
  33.             }
  34.         }
  35.        
  36.         in.close();
  37.     }
  38.  
  39. }
Add Comment
Please, Sign In to add comment