Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cmd.input;
- import java.util.Scanner;
- public class CMDInput {
- public static void main(String[] args) {
- int temp;
- System.out.print("Type ASC for ascending or DESC for descending order: ");
- Scanner s = new Scanner(System.in);
- String s1 = s.nextLine();
- int a[];
- a = new int[5];
- System.out.println("Enter all the elements:");
- for (int i = 0; i < 5; i++)
- {
- a[i] = s.nextInt();
- }
- if (s1.equals("ASC"))
- {
- for (int i = 0; i <5; i++)
- {
- for (int j = i + 1; j <5; j++)
- {
- if (a[i] > a[j])
- {
- temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- }
- }
- }
- System.out.print("Ascending Order:");
- for (int i = 0; i < 5 - 1; i++)
- {
- System.out.print(a[i] + ",");
- }
- System.out.print(a[5 - 1]);
- }
- else if (s1.equals("DESC"))
- {
- for (int i = 0; i < 5; i++)
- {
- for (int j = i + 1; j < 5; 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 < 5 - 1; i++)
- {
- System.out.print(a[i] + ",");
- }
- System.out.print(a[5 - 1]);
- }
- else
- System.out.println("Invalid input");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement