Java Tutorial
Reads the number of elements in the array, elements and finds the second smallest element by sorting array in ascending order.
import java.util.*; class Small { public static void main(String args[]) { int n; int i; int j; int t; int []a=new int[10]; Scanner Sc=new Scanner(System.in); System.out.println("Enter the no of elements: "); n=Sc.nextInt(); System.out.println("Enter the elements: "); for(i=0;i<n;i++) { a[i]=Sc.nextInt(); } //Finding second smallest element in an array for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i] > a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } System.out.println("Second_smallest is "+a[1]); } }Output:
D:\Java_Programs>javac Small.java D:\Java_Programs>java Small Enter the no of elements: 5 Enter the elements: 32 43 23 65 78 Second_smallest is 32
Reads the number of elements in the array, elements and finds the second biggest element by sorting array in descending order.
import java.util.*; class Small { public static void main(String args[]) { int n; int i; int j; int t; int []a=new int[10]; Scanner Sc=new Scanner(System.in); System.out.println("Enter the no of elements: "); n=Sc.nextInt(); System.out.println("Enter the elements: "); for(i=0;i<n;i++) { a[i]=Sc.nextInt(); } //Finding second biggest element in an array for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i] < a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } System.out.println("Second_smallest is "+a[1]); } }Output:
D:\Java_Programs>javac Biggest.java D:\Java_Programs>java Biggest Enter the no of elements: 5 Enter the elements: 3 435 4 354 4 Second_smallest is 354
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page