-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
44 lines (39 loc) · 1.03 KB
/
Fibonacci.java
File metadata and controls
44 lines (39 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.Scanner;
public class Fibonacci
{
static void Calculate (int n)
{
int result = 0;
int summand1 = 0;
int summand2 = 1;
for (int i = 1; i<n; i++)
{
result = summand1 + summand2;
summand1 = summand2;
summand2 = result;
}
if(n==1)
{
System.out.println("Das Ergebnis der Fibonacci-Folge lautet: 1");
}
else
{
System.out.println("Das Ergebnis der Fibonacci-Folge lautet: " + result);
}
}
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Geben Sie die Länge n ihrer Fibonacci-Folge ein: ");
int n = 0;
n = scanner.nextInt();
if(n<0)
{
System.out.println("Negative Zahl eingegeben!");
System.out.println("Programm wird beendet...");
System.exit(0);
}
scanner.close();
Calculate(n);
}
}