-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChecking_The_Given_Numbers_Are_In_What_Series.java
More file actions
115 lines (103 loc) · 2.18 KB
/
Checking_The_Given_Numbers_Are_In_What_Series.java
File metadata and controls
115 lines (103 loc) · 2.18 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package techincals;
/*Checking the given numbers are in what kind of series i.e in AP or GP or Fibonacci and printing the n +1 term of that series*/
import java.util.*;
public class Series {
static boolean ap(int n , int [] ar)
{
int flag=0;
for(int i =1;i<n-1;i++)
{
if(((ar[i-1]+ar[i+1])/2==ar[i])&&(ar[i+1]-ar[i]==ar[i]-ar[i-1]))
flag=1;
else
{
flag=0;
break;
}
}
if(flag==1)
return true;
else
return false;
}
static boolean gp(int n , int [] ar)
{
int flag =0;
for(int i =1;i<n-1;i++)
{
int r = ar[i]/ar[i-1];
if(ar[i]*r==ar[i+1])
flag=1;
else
{
flag=0;
break;
}
}
if(flag==1)
return true;
else
return false;
}
static boolean fibo(int n , int [] ar)
{
int flag=0;
for(int i =1;i<n-1;i++)
{
if((ar[i-1]+ar[i]==ar[i+1])&& (ar[i+1]-ar[i]==ar[i-1]))
flag=1;
else
{
flag=0;
break;
}
}
if(flag ==1)
return true;
else
return false;
}
static void check(int n , int [] ar)
{
try
{
if(ap(n,ar))
{
System.out.println("The Given numbers are in Arithmetic Progression or AP Series");
System.out.println("The n +1 term of the series is :"+(ar[n-1]+(ar[n-1]-ar[n-2])));
}
if(gp(n,ar))
{
System.out.println("The Given numbers are in Geometric Progression or GP Series");
System.out.println("The n +1 term of the series is :"+(ar[n-1]*(ar[n-1]/ar[n-2])));
}
if(fibo(n,ar))
{
System.out.println("The Given numbers are in Fibonacci Series");
System.out.println("The n +1 term of the series is :"+(ar[n-1]+ar[n-2]));
}
else
{
System.out.println("The given numbers are not in any kind of series");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Size of the array");
int n = sc.nextInt();
int [] ar = new int[n];
System.out.println("Enter the numbers in the array");
for(int i =0;i<n;i++)
{
ar[i]=sc.nextInt();
}
System.out.println(Arrays.toString(ar));
check(n,ar);
}
}