Good Morning Friends! Today I am going to give another tutorial on array. Today's tutorial will be based on mainly to find out largest and smallest number in the given array. If you are beginner, then I recommend you to go to my previous tutorial on array such that you can easily understand today's tutorial. Here is the link JAVA PROGRAMMING TUTORIAL 28: SUMMATION OF ELEMENTS OF ARRAY . Now follow the below code to learn to find out the largest and smallest number. If you get any doubt then please write on the comment box.
public class HackDefence {
public static void main(String[] args)
{
double[] array = {9,8,7.65,100.31,34,90,100.13};
for(double numbers: array){
System.out.println(numbers);
}
double SUM = 0;
for (int i = 0; i < array.length; i++) {
SUM += array[i];
}
System.out.println("Sum of all the numbers in the array = " + SUM);
double smallestNumber = array[0];
for(int i=1; i<array.length; i++){
if(array[i]<smallestNumber)
smallestNumber = array[i];
}
System.out.println("Smallestt number in the array = " + smallestNumber);
double largestNumber = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > largestNumber)
largestNumber = array[i];
}
System.out.println("Largest number in the array = " + largestNumber);
}
}
(my facebook profile link: https://www.facebook.com/dipankar.choudhury1)
OUTPUT:
No comments:
Post a Comment