micher43

P6.2

Oct 24th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class fibonacchiAlgorithm {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         int fold1 = 1;
  9.         int fold2 = 1;
  10.         int foldNew = fold1 + fold2;
  11.        
  12.        
  13.         Scanner s = new Scanner(System.in);
  14.         System.out.println("enter an integer n");
  15.         int nthPosition = s.nextInt();
  16.        
  17.         if(nthPosition == 1)
  18.         {
  19.             foldNew = 1;
  20.         }
  21.         else if(nthPosition == 2)
  22.         {
  23.             foldNew = 1;
  24.         }
  25.         else if(nthPosition == 3)
  26.         {
  27.             foldNew = 2;
  28.         }
  29.         else if(nthPosition > 3)
  30.         {
  31.            
  32.             for(int i = 1; i <= (nthPosition - 3); i++)
  33.                 {
  34.                     fold2 = fold1;
  35.                     fold1 = foldNew;
  36.                     foldNew = fold2 + fold1;
  37.                 }
  38.                
  39.         }
  40.         System.out.println(foldNew);
  41.     }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment