This repository has been archived by the owner on Oct 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 766
/
Samarth
73 lines (61 loc) · 2.04 KB
/
Samarth
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
Name: Samarth Kumar
Github username: Samarthku
Date: 01/sep/2023
Program Lang: Java
Program Name: Complex Array Program
(To find the difference between the maximum and minimum number in an array.)
Info:- Below we have implemented a one-dimensional array in Java where we have created a user-defined class:
Solution: The task we have to complete is to find the minimum value, maximum value and then finally find the difference between the max and min value. The code will be as follows:
// Code Start
import java.util.Scanner;
//This class will calculate the max and min values of the array
class TestArray
{
int MAX(int[]Arry)
{
int maxValue= Arry[0];
for(int i=1;i<Arry.length;i++)
{
if(Arry[i]>maxValue)
{
maxValue=Arry[i];
}
}
return maxValue;//This method will return the max value present in the array.
}
int MIN(int[]Arry)
{
int minValue=Arry[0];
for(int i=1;i<Arry.length;i++)
{
if(Arry[i]<minValue)
{
minValue=Arry[i];
}
}
return minValue;
}
}
public class DifferenceArry
{
public static void main(String[] args)
{
int n;
//It creates scanner object
Scanner sc = new Scanner(System.in);
System.out.print("Enter the array elements:" );
n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<arr.length;i++)
{
System.out.print("Enter ["+(i+1)+"] element :" );
arr[i]=sc.nextInt();
}
TestArray obj=new TestArray();
System.out.println("Maximum value in the array is :" +obj.MAX(arr));
System.out.println("Minimum value in the array is :" +obj.MIN(arr));
int diff=obj.MAX(arr)-obj.MIN(arr);
System.out.print("Difference between max and min elements is : " +diff );
}
}
// code End