Input 10 numbers in 1d array and find largest and the smallest number position in Java
One Dimensional Array - Question 3
In this question, we will see how to input 10 numbers in a one dimensional integer array and find the position of the largest and the smallest number in Java programming. To know more about one dimensional array click on the one dimensional array lesson.
Q3) Write a program in Java to input 10 numbers in a one dimensional integer array and find the position of the largest and the smallest number.
Program
import java.util.Scanner;
public class Q3
{
public static void main(String args[])
{
int a[]=new int[10], i,ln=0,lnp=0,sn=0,snp=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 10 numbers");
for(i=0; i<10; i++)
{
a[i]=sc.nextInt();
}
for(i=0; i<10; i++)
{
if(i==0)
{
ln=a[i];
sn=a[i];
lnp=i;
snp=i;
}
else if(a[i]>ln)
{
ln=a[i];
lnp=i;
}
else if(a[i]<sn)
{
sn=a[i];
snp=i;
}
}
System.out.println("Largest Number Position = "+(lnp+1));
System.out.println("Smallest Number Position = "+(snp+1));
}
}
Output
Enter 10 numbers 63 12 57 61 98 11 3 4 75 82 Largest Number Position = 5 Smallest Number Position = 7