Java program to print the ascending order using for loop

Java program to print the ascending order using for loop. In the java programming language, an ascending order program is used to arrange the numbers that are entered in an array. The best method for developing programming skills in any programming language is a sorting program.

import java.util.Scanner;
public class Main 
{
public static void main(String[] args) 
{
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter the size of an array");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter 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("After sorting");
for (int i = 0; i < n - 1; i++) 
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}

 

Output:

Enter the size of an array 3

Enter the elements

34 56 78

After sorting34,56,78