Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main
- {
- public static void main(String[]args)
- {
- int a = 1, b = 1, n = 0;
- Scanner in = new Scanner(System.in);
- boolean IsCorrect;
- System.out.print("The program will calculate the n-th term of the Fibonacci series.\n");
- System.out.print("Enter the Fibonacci series member number: ");
- do
- {
- IsCorrect = false;
- try {
- n = Integer.parseInt(in.next());
- }
- catch (NumberFormatException e) {
- System.out.print("Incorrect value. Enter the number: ");
- IsCorrect = true;
- }
- if ((!IsCorrect) && (n == 0) || (n < 0))
- {
- System.out.print("Incorrect value. Enter the number: ");
- IsCorrect = true;
- }
- } while (IsCorrect);
- in.close();
- for (int i = 2; i < n; i++)
- {
- b = a + b;
- a = b - a;
- }
- System.out.print(b);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement