jTruBela

Fibonacci

Feb 12th, 2021 (edited)
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. // ***************************************************************************************/
  2. // File Name: JRT_Fibonacci.java            Author: Justin Trubela                        /        
  3. // Section: 03                                                                            /
  4. //                                                                                        /
  5. // Purpose:       4.Write an array that contains the first 20 using a calculation         /
  6. //                      to generate the numbers                                           /
  7. //The program prints the first 20 Fibonacci numbers in the array                                XX
  8. //                                                  separated by a comma and a space.           XX
  9. //
  10. //Asks the user which of the numbers in this series of 20 he wants to see                       XX
  11. //      and prompts for an integer input - a number between 1 and 20(inclusive).
  12. //      So if the user wants to see the fifth (5th) number of the Fibonacci series
  13. //          the user would input the integer 5 in response to the prompt.
  14. //
  15. //Checks that the user has not input a number lower than 1 or higher than 20                    XX
  16. //
  17. //Prints in response to the user entry "The nth Fibonacci number is  X",                        XX
  18. //     where n is the number input by the user and X is the  nth Fibonacci number.
  19. //     (Array indexes for the elements of the array start at 0),
  20. //     Example: If user inputs "6" in response to the prompt,
  21. //    the program would print "The 6th Fibonacci number is 5." (without the quotes)
  22. //****************************************************************************************/
  23.  
  24. import java.util.Scanner;
  25.  
  26. public class JRT_Fibonacci {
  27.     public static void main (String[] args) {
  28.    
  29.         Scanner scan = new Scanner(System.in);
  30.         int limit = 20;
  31.         long[] series = new long[limit];
  32.    
  33.         //create first 2 series elements
  34.         series[0] = 0;
  35.         series[1] = 1;
  36.    
  37.         long f1=0,f2=1,f3;
  38.    
  39.         for (int i= 0; i<20; i++) {
  40.             f3=f1+f2;
  41.             series[i]=f3;
  42.             f1=f2;
  43.             f2=f3;
  44.             if(i<series.length-1) {
  45.                 System.out.print(series[i]+", ");
  46.                 }
  47.             else {
  48.                 System.out.print(series[i]+".");
  49.             }
  50.  
  51.         }
  52.         System.out.println("\n\nEnter the number of the Fibonacci Series would you like to see:");
  53.         int userInputFibNum = scan.nextInt();
  54.         while (userInputFibNum < 1 || userInputFibNum > 20) {
  55.             System.out.println("Number must be between 1 and 20");
  56.             System.out.println("\n\nEnter the number of the Fibonacci Series would you like to see:");
  57.             userInputFibNum = scan.nextInt();
  58.         }
  59.        
  60.         String postfix = "th";
  61.         if(userInputFibNum == 1) {
  62.             postfix = "st";
  63.         }
  64.         else if(userInputFibNum == 2) {
  65.             postfix = "nd";
  66.         }
  67.         else if(userInputFibNum == 3) {
  68.             postfix = "rd";
  69.         }
  70.  
  71.         System.out.println("The "+userInputFibNum + postfix + " Fibonacci number is: " + series[userInputFibNum-1]);
  72.        
  73.        
  74.         scan.close();
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment