-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayingwitharrays
125 lines (80 loc) · 3.02 KB
/
playingwitharrays
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
116
117
118
119
120
121
122
123
124
125
package revaturedaythree;
//by Haley Moses for revature class
import java.util.Scanner;
import java.util.Arrays;
public class playingwitharrays
{
public static void main (String[]args)
{
{
//find all even numbers and put in new array
// and all odd numbers and put in new array
//return the minimum and maximum values elements of the array
//check to see if any of the array exhibits palamidromic behavior
//scanner input to generate the array number of elements and element values
int n; int i = 0; int y = 0;
@SuppressWarnings("resource")
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array: ");
n = s.nextInt();
//n is the numerical element value, i is the element position and the array is defined as a[i]
int[] a = new int[n];
System.out.println("Enter all the elements: ");
for (i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
int arrsize=a.length;
//going to check to see if the array has palaindromic behavior
{
int x = 0;
//looping till array a is size n/2
for (x = 0; x <= n / 2 && n != 0; x++)
{
//checking if first and last element are different then set x to 1
if (a[x] !=a[n-x-1])
{
x = 1;
break;
}
}
//if they are different than print not palindrome else print palindrome
if (x==1)
System.out.println("Not Palindrome ");
else
System.out.println("Palindrome ");
}
//going to sort the array in ascending order and call the positions of the first and last as my min and max values
{
Arrays.sort(a);
//checking for if there is less than 2 inputs
if (arrsize < 2)
{
System.out.println("Invalid Input ");
return;
}
else
{
int first = a[0];
//maximum array value at the first index of the array
int second = a[(arrsize-1)];
//minimum array value at the last index of the array
System.out.println("The minimum value of the array is: "+first);
System.out.println("The maximum value of the array is "+second);
}
int sumeven = 0; int sumodd = 0;
for (i=0; i<n; i++)
{
if( a[i] % 2 == 0 )
{
sumeven = sumeven +a[i];
}
else
sumodd = sumodd +a[i];
}
System.out.println("Sum of the odd numbers array elements is: "+sumodd);
System.out.println("Sum of the even numbers array elements is: "+sumeven);
}
}
}
}