Today , ARRAYS were discussed in the class. An Array is a group of contiguous or related data items that share a common name. It enables us to develop concise and efficient programs. Arrays can be of any variable type. This lecture covered - Creating arrays, Declaration of arrays, Initialization of arrays , One Dimensional arrays, Multidimensional arrays , etc.
Examples:
1. Java code for finding the Largest & Smallest number in an Array
import java.util.*;
public class test1
{
public static void main(String [ ] args)
{
int i;
int numbers []=new int[]{4,65,89,34,71,70,49,22,56,13};
int smallest= numbers[0];
int largest= numbers[0];
for(i=0;i<numbers.length;i++)
{
if(numbers[i]>largest)
largest = numbers[i];
else if(numbers[i]<smallest)
smallest=numbers[i];
}
System.out.println("The largest number in this array is:" + largest);
System.out.println("The smallest number in this array is:" + smallest);
}
}
OUTPUT:
The largest number in this array is:89
The smallest number in this array is:4
Examples:
1. Java code for finding the Largest & Smallest number in an Array
import java.util.*;
public class test1
{
public static void main(String [ ] args)
{
int i;
int numbers []=new int[]{4,65,89,34,71,70,49,22,56,13};
int smallest= numbers[0];
int largest= numbers[0];
for(i=0;i<numbers.length;i++)
{
if(numbers[i]>largest)
largest = numbers[i];
else if(numbers[i]<smallest)
smallest=numbers[i];
}
System.out.println("The largest number in this array is:" + largest);
System.out.println("The smallest number in this array is:" + smallest);
}
}
OUTPUT:
The largest number in this array is:89
The smallest number in this array is:4
No comments:
Post a Comment