Advertisement
LisunovaMaryna

lab1.2 java

Sep 26th, 2023 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Main
  3. {
  4.     public static void main(String[]args)
  5.     {
  6.         int a = 1, b = 1, n = 0;
  7.         Scanner in = new Scanner(System.in);
  8.         boolean IsCorrect;
  9.         System.out.print("The program will calculate the n-th term of the Fibonacci series.\n");
  10.         System.out.print("Enter the Fibonacci series member number: ");
  11.         do
  12.         {
  13.             IsCorrect = false;
  14.             try {
  15.                 n = Integer.parseInt(in.next());
  16.             }
  17.             catch (NumberFormatException e) {
  18.                 System.out.print("Incorrect value. Enter the number: ");
  19.                 IsCorrect = true;
  20.             }
  21.             if ((!IsCorrect) && (n == 0) || (n < 0))
  22.             {
  23.                 System.out.print("Incorrect value. Enter the number: ");
  24.                 IsCorrect = true;
  25.             }
  26.         } while (IsCorrect);
  27.         in.close();
  28.         for (int i = 2; i < n; i++)
  29.         {
  30.             b = a + b;
  31.             a = b - a;
  32.         }
  33.         System.out.print(b);
  34.  
  35.     }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement