Program:-
import java.util.Scanner;
public class p5a{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int i,j,n;
System.out.println("enter the size of array:");
n=s.nextInt();
int a[]=new int[n];
System.out.println("enter the element:");
for(i=0;i<n;i++) {
a[i]=s.nextInt();
}
for(i=0;i<n;i++) {
for(j=i+1;j<n;j++) {
if(a[i]>a[j]) {
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("ascending order:");
for(i=0;i<n;i++){
System.out.print(a[i]+ " ");
}
System.out.println();
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]<a[j]){
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("descending order:");
for(i=0;i<n;i++){
System.out.print(a[i]+ " ");
}
System.out.println();
}
}
Output:-
import java.util.Scanner;
public class p5a{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int i,j,n;
System.out.println("enter the size of array:");
n=s.nextInt();
int a[]=new int[n];
System.out.println("enter the element:");
for(i=0;i<n;i++) {
a[i]=s.nextInt();
}
for(i=0;i<n;i++) {
for(j=i+1;j<n;j++) {
if(a[i]>a[j]) {
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("ascending order:");
for(i=0;i<n;i++){
System.out.print(a[i]+ " ");
}
System.out.println();
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]<a[j]){
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("descending order:");
for(i=0;i<n;i++){
System.out.print(a[i]+ " ");
}
System.out.println();
}
}
Output:-
No comments:
Post a Comment