-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution for Question_2.java
55 lines (48 loc) · 1.27 KB
/
Solution for Question_2.java
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
/*
Question:Write a program that prompts the user to enter a list and displays whether the list is sorted
or not.
*/
//Solution
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] list1 =new int[5];
int[] templist =new int[5];
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the elements of the list: ");
int n=0;
while (n<5){
list1[n]=scanner.nextInt();
n++;
}
for (int i=0;i<n;i++){
templist[i]=list1[i];
}
for (int i=0;i<n;i++){
for (int j=i+1;j<n;j++){
if(templist[i]>templist[j]){
int temp;
temp=templist[i];
templist[i]=templist[j];
templist[j]=temp;
}
}
}
int check=1;
for (int i=0;i<n;i++){
if (list1[i]==templist[i]){
}
else {
check=0;
}
}
if(check==0){
System.out.println("Array is not sorted!");
}
else {
System.out.println("Array is Sorted");
}
}
}