-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment_27_Array.java
More file actions
117 lines (101 loc) · 2.87 KB
/
Assignment_27_Array.java
File metadata and controls
117 lines (101 loc) · 2.87 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.util.Scanner;
/*Q1:Write a program using array accept the size from the user than accordingly
accept the no from the user and find out the greater no,smaller no, ascending, Descending
*/
public class Assignment_27_Array
{
public static void main(String[] args)
{
Assignment_27_Array aa=new Assignment_27_Array();
int arr[]=aa.accept();
aa.disp(arr);
aa.Maximum(arr);
aa.Minimum(arr);
int num[]=aa.Assending();
aa.Descending(num);
}
public int[] accept()
{
Scanner sc=new Scanner(System.in);
int size,i;
System.out.println("Enter the size");
size=sc.nextInt();
int number[]=new int[size];
System.out.println("Enter "+size+" Numbers:");
for(i=0;i<size;i++)
{
number[i]=sc.nextInt();
}
return number;
}
public void disp(int number[])
{
int i;
for(i=0;i<number.length;i++)
{
System.out.println(number[i]);
}
}
public void Maximum(int a[])
{
int i;
int max = a[0];// Initialize maximum element for find max
for (i = 1; i < a.length; i++)// compare every element with current max
if (a[i] > max)
max = a[i];
System.out.println("greater number is = "+max);;
}
public void Minimum(int a[])
{
int i;
int min = a[0];// Initialize minimum element for find min
for (i = 1; i < a.length; i++)// compare every element with current min
if (a[i] < min)
min = a[i];
System.out.println("Smaller number is = "+min);
}
private int[] Assending()
{
int num[]=accept();
int temp=0,i;
for(i=0;i<num.length;i++)
{
for(int j=i+1;j<num.length;j++)
{
if(num[i] > num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
System.out.println("Elements of array in Ascending order: ");
for (i=0;i<num.length;i++)
{
System.out.print(num[i] + "\t ");
}
return num;
}
private void Descending(int num[])
{
int temp=0,i;
for(i=0;i<num.length;i++)
{
for(int j=i+1;j<num.length;j++)
{
if(num[i] < num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
System.out.println("\nElements of array in Descending order: ");
for (i=0;i<num.length;i++)
{
System.out.print(num[i] + "\t ");
}
}
}