Write a Java Program to Sort the Array in Descending Order


Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. import java.util.Scanner;
  2. public class Javaexcercise
  3. {
  4. public static void main(String[] args)
  5. {
  6. int n, temp;
  7. Scanner s = new Scanner(System.in);
  8. System.out.print("Enter no. of elements: ");
  9. n = s.nextInt();
  10. int a[] = new int[n];
  11. System.out.println("Enter all the elements: ");
  12. for (int i = 0; i < n; i++)
  13. {
  14. a[i] = s.nextInt();
  15. }
  16. for (int i = 0; i < n; i++)
  17. {
  18. for (int j = i + 1; j < n; j++)
  19. {
  20. if (a[i] < a[j])
  21. {
  22. temp = a[i];
  23. a[i] = a[j];
  24. a[j] = temp;
  25. }
  26. }
  27. }
  28. System.out.print("Descending Order:");
  29. for (int i = 0; i < n - 1; i++)
  30. {
  31. System.out.print(a[i] + ",");
  32. }
  33. System.out.print(a[n - 1]);
  34. }
  35. }

Result

java techstudy.org

     





Related Topics




Popular Posts