Skip to content

Commit bfbee08

Browse files
Create CArray.java
1 parent 1cfb410 commit bfbee08

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

CArray.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import java.util.Scanner;
2+
//This class will calculate the max and min values of the array
3+
class TestArray
4+
{
5+
6+
int MAX(int[]Arry)
7+
{
8+
int maxValue= Arry[0];
9+
10+
for(int i=1;i<Arry.length;i++)
11+
{
12+
if(Arry[i]>maxValue)
13+
{
14+
maxValue=Arry[i];
15+
}
16+
}
17+
return maxValue;//This method will return the max value present in the array.
18+
}
19+
20+
int MIN(int[]Arry)
21+
{
22+
int minValue=Arry[0];
23+
24+
for(int i=1;i<Arry.length;i++)
25+
{
26+
if(Arry[i]<minValue)
27+
{
28+
minValue=Arry[i];
29+
}
30+
}
31+
return minValue;
32+
}
33+
}
34+
35+
public class DifferenceArry
36+
{
37+
public static void main(String[] args)
38+
{
39+
int n;
40+
41+
//It creates scanner object
42+
Scanner sc = new Scanner(System.in);
43+
System.out.print("Enter the array elements:" );
44+
n=sc.nextInt();
45+
46+
int arr[]=new int[n];
47+
48+
for(int i=0;i<arr.length;i++)
49+
{
50+
System.out.print("Enter ["+(i+1)+"] element :" );
51+
arr[i]=sc.nextInt();
52+
}
53+
54+
TestArray obj=new TestArray();
55+
System.out.println("Maximum value in the array is :" +obj.MAX(arr));
56+
System.out.println("Minimum value in the array is :" +obj.MIN(arr));
57+
int diff=obj.MAX(arr)-obj.MIN(arr);
58+
System.out.print("Difference between max and min elements is : " +diff );
59+
}
60+
}

0 commit comments

Comments
 (0)