Computer Science, asked by somagan4, 1 year ago

Java program to arrange 5 number entered by user in ascending order

Answers

Answered by Arjunsharma365
1
hllo frnd here is ur answer☺✌_________________
___________________________
This is a Java Program to Sort the Array in an Ascending 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 ascending order.

Here is the source code of the Java Program to Sort the Array in an Ascending Order. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

hope it helps u☺☺

somagan4: I need the program not explanation
Answered by Prashant24IITBHU
2
Here is your program

This program will work for both
1. To sort an array as well as
2. To arrange 5 numbers

import java.util.Scanner;
public class Ascending _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("Ascending Order:");

       for (int i = 0; i < n - 1; i++)
        {
            System.out.print(a[i] + ",");
       }

       System.out.print(a[n - 1]);
    }
}
Similar questions