Skip to content

Commit 01189ab

Browse files
committed
M3P3
1 parent 47fc73a commit 01189ab

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

module3/ArrayMinMax.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//Program to find the maximum and minimum value in an array of size m passed as argument.
2+
import java.util.Scanner;
3+
4+
public class ArrayMinMax
5+
{
6+
public static void main(String args[]) {
7+
int size = Integer.parseInt(args[0]);
8+
System.out.print("\nSize of Array is: " + size);
9+
int[] arr = new int[size]; // Declare an array variable
10+
Scanner reader = new Scanner(System.in);
11+
System.out.print("\nEnter the elements of the array --------");
12+
for(int i = 0; i < size; i++ )
13+
{
14+
System.out.print("\nEnter the "+ (i+1) + " element = ");
15+
arr[i] = reader.nextInt();
16+
}
17+
System.out.print("\nEntered array is ----------\n");
18+
for (int i = 0; i < size ;i++)
19+
{
20+
System.out.print("\t"+arr[i]);
21+
}
22+
// Code for finding Maximum and Minimum by appliyning linear search
23+
int max = arr[0];
24+
int min = arr[0];
25+
for(int i = 1; i<size; i++)
26+
{
27+
if (max<arr[i])
28+
{
29+
max = arr[i];
30+
}
31+
if (min>arr[i])
32+
{
33+
min = arr[i];
34+
}
35+
}
36+
System.out.print("\nMaximum of the array is: " + max);
37+
System.out.print("\nMinimum of the array is: " + min);
38+
}
39+
}

0 commit comments

Comments
 (0)