Advertisement
JoshJurban

Project 6.2

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