Write a program to sort a number in ascending order
import java.util.Scanner; public class SortNumbersInAscendingOrder { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter Number of elements:-"); int limit=sc.nextInt(); System.out.println(" Enter Elements:- "); int a[]=new int[10]; for(int i=1;i<=limit;i++) { a[i]=sc.nextInt(); } for(int i=0;i<limit-1;i++) for(int j=i;j<limit;j++) if(a[i]>a[j]) { int temp=a[i]; a[i]=a[j]; a[j]=temp; } System.out.println("The sorted list is:-"); for( int i=0;i<limit;i++) System.out.print(" "+a[i]); } } OUTPUT: Enter Number of elements:- 5 Enter Elements:- 15 25 16 12 4 The sorted list is:- 0 12 15 16 25
No comments:
Post a Comment