forked from Krushna-Prasad-Sahoo/JavaCode-Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci.java
68 lines (54 loc) · 1.7 KB
/
fibonacci.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.Scanner;
/**
*
* @author Lakshitha Samod
* I Created a java program for the display Fibonacci Series. Also, I have implemented the below functions.
* -Take input the value of 'n', up to which you will print.
* -Print the Fibonacci Series up to n while replacing prime numbers, all multiples of 5 by 0.
*/
public class Main {
public static void main(String[] args) {
//define variables
int no, t1 = 1, t2 = 1, next = 0;
//get input from user
Scanner sc = new Scanner(System.in);
no = sc.nextInt();
// print Fibonacci numbers
for (int i = 1; i < (no + 1); i++) {
// print first term
if (i == 1) {
System.out.print(t1 + " ");
continue;
}
// print second term
if (i == 2) {
System.out.print(t2 + " ");
continue;
}
next = t1 + t2;
t1 = t2;
t2 = next;
if (isPrime(next) || isMulOfFive(next))
System.out.print("0" + " ");
else
System.out.print(next + " ");
}
}
//check wheteher no is a prim e one
public static boolean isPrime(int n) {
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
//check wheteher no is a multiple of 5
public static boolean isMulOfFive(int x) {
if (x % 5 == 0)
return true;
else
return false;
}
}