-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCC3p2q4.java
More file actions
48 lines (41 loc) · 1.44 KB
/
CC3p2q4.java
File metadata and controls
48 lines (41 loc) · 1.44 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
// Get input for size an array
// Get input for each element in an array
// Find and print element in an array
import java.util.Scanner;
class CC3p2q4{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
// STEP 1: Get input for the size of an array
System.out.print("Enter the size of the array: ");
int size = scan.nextInt();
// STEP 2: Create an array
int[] array = new int[size];
// STEP 3: Get input for each element in the array using for loop
for(int i = 0; i < size; i++)
{
System.out.print("Enter number " + (i + 1) + " : ");
array[i] = scan.nextInt();
}
// STEP 4: Printing the whole array for reference using for loop
System.out.print("\n");
for(int i = 0; i < size; i++)
{
System.out.println("Array of " + i + " : " + array[i]);
}
System.out.print("\n");
// STEP 5: To find the middle element in the array
int k = array.length;
int mid = k / 2;
// STEP 6: Print the middle element for both odd and even cases
if(k % 2 != 0)
{
System.out.println("Middle element: " + array[mid]);
}
else
{
System.out.print("Middle elements: " + array[mid - 1]);
System.out.print(" And " + array[mid]);
}
}
}