Top 10 Array Coding Question For Interview:
(1)Write a program to find smaller element in Array
Solution: public class SmallerElementInArray
{
public static void main (String[] args)
{
int[] arr={5,4,3,2,1};
int min=arr[0];
for(int i=1;i<arr.length;i++)
{
if(min>arr[i])
min=arr[i];
}
System.out.println("Smaller Element is: "+min);
}
}
(2)Write a program to find Largest Element in Array:
Soluition: public class LargestElementInArray
{
public static void main (String[] args)
{
int[] arr={5,4,3,2,1};
int max=arr[0];
for(int i=1;i<arr.length;i++)
{
if(max<arr[i])
max=arr[i];
}
System.out.println("Largest Element is: "+max);
}
}
No comments:
Post a Comment