This is a Java Program to Sort the Array in Descending Order.
Enter size of array and then enter all the elements of that array. Now with the help of for loop and temp variable we sort the array in descending order.
Here is the source code of the Java Program to Sort the Array in Descending Order. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Descending_Order
{
public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Descending Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}
Output:
$ javac Descending_Order.java $ java Descending_Order Enter no. of elements you want in array:5 Enter all the elements: 2 3 5 1 4 Descending Order:5,4,3,2,1
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Apply for Java Internship
- Practice BCA MCQs
- Check Programming Books
- Apply for Computer Science Internship
- Practice Information Technology MCQs