-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain1.java
61 lines (51 loc) · 1.05 KB
/
Main1.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
import java.io.*;
import java.util.*;
class PrimeThread extends Thread {
public boolean prime(int no)
{
int c=0;
for(int i=2;i<no;i++)
{
if(no%i==0)
c++;
}
if(c==0)
return true;
return false;
}
}
class FibonacciThread extends Thread {
public boolean fibonacci(int n)
{
int n1=0,n2=1,n3;
for(int i=0;i<n;i++)
{
n3=n1+n2;
if(n==n3)
return true;
n1=n2;
n2=n3;
}
return false;
}
}
public class Main1 {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
PrimeThread pt= new PrimeThread();
FibonacciThread ft=new FibonacciThread();
System.out.println("Enter the value of N: ");
int n=sc.nextInt();
List<Integer>list = new ArrayList<Integer>();
System.out.println("Numbers that are both prime and Fibonacci:");
for(int i=2;i<=n;i++)
{
boolean flag1 = pt.prime(i);
boolean flag2 = ft.fibonacci(i);
if(flag1 && flag2)
list.add(i);
}
System.out.println(list);
}
}