Repository navigation
Expand file tree
/
Copy pathLabtaskjava.java
More file actions
97 lines (76 loc) · 2.7 KB
/
Copy pathLabtaskjava.java
File metadata and controls
97 lines (76 loc) · 2.7 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
import java.sql.SQLOutput;
import java.util.Arrays;
import java.util.Scanner;
public class Labtaskjava {
public static void main(String[] args) {
int[] nums;
System.out.println("enter the number of elements in the array");
Scanner sc = new Scanner(System.in);
nums = new int[sc.nextInt()];
System.out.println("enter the elements in the array");
double sum = 0;
double avg = 0;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int min2 = min;
int max2 = max;
for (int i = 0; i < nums.length; i++) {
nums[i] = sc.nextInt();
sum += nums[i];
avg = sum / nums.length;
if (min > nums[i]) {
min2 = min;
min = nums[i];
} else if (min2 > nums[i]) {
min2 = nums[i];
}
if (max < nums[i]) {
max2 = max;
max = nums[i];
} else if (max2 < nums[i]) {
max2 = nums[i];
}
}
System.out.println(Arrays.toString(nums));
System.out.println("sum = " + sum);
System.out.println("avg = " + avg);
System.out.println("min = " + min);
System.out.println("max = " + max);
System.out.println("secondmax = " + max2);
System.out.println("secondmin = " + min2);
// palindrome check
Scanner sc2 = new Scanner(System.in);
System.out.println("enter a number or a word to check if it's a palindrome: ");
String word = sc2.nextLine();
String reverse = "";
for (int i = word.length() - 1; i >= 0; i--) {
reverse += word.charAt(i);
}
System.out.println("reversed input = " + reverse);
if (word.equals(reverse)) {
System.out.println("palindrome :)");
}
else{
System.out.println("not palindrome :((( ");
}
Scanner sc3 = new Scanner(System.in);
System.out.println("enter a sentence to check if it's a palindrome: ");
String sentence = sc3.nextLine();
System.out.println("sentence = " + sentence);
String[] words = sentence.split(" ");
String reverseSentence = "";
for (int i = words.length - 1; i >= 0; i--) {
reverseSentence += words[i];
if (i != 0) {
reverseSentence += " ";
}
}
System.out.println("reversed sentence = " + reverseSentence);
if (sentence.equals(reverseSentence)) {
System.out.println("palindrome sentence:)");
}
else{
System.out.println("not a palindrome sentence:(( ");
}
}
}