how does the array is finding the smallest and largest number itself? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how does the array is finding the smallest and largest number itself?

public static void main(String args[]) {int nums[] = new int[10];int min, max;nums[0] = 99;nums[1] = -10;nums[2] = 100123;nums[3] = 18;nums[4] = -978;nums[5] = 5623;nums[6] = 463;nums[7] = -9;nums[8] = 287;nums[9] = 49;Chapter 5: More Data Types and Operators 139min = max = nums[0];for(int i=1; i < 10; i++) {if(nums[i] < min) min = nums[i];if(nums[i] > max) max = nums[i];}System.out.println("min and max: " + min + " " + max);}} why its not printing the first smallest no it gets in first if statement? like-10.. explain plz

25th Jul 2016, 7:32 PM
ankit balodhi
ankit balodhi - avatar
5 Answers
+ 2
it's not printing the first smallest number because that number is replaced with a smaller number before you ask for it to be printed to the screen.
25th Jul 2016, 8:01 PM
Barra
Barra - avatar
0
yeah you are right Bod
26th Jul 2016, 2:11 PM
Prashant
Prashant - avatar
0
u need to put the println inside the for loop
26th Jul 2016, 11:21 PM
Hong Wei
Hong Wei - avatar
0
you can use Arrays.sort(*your Array*); to sort your Array
27th Jul 2016, 4:32 PM
FreakManMega
0
why not iterate using I< num.length? or than I<10
1st Aug 2016, 9:36 AM
Daniel Ani
Daniel Ani - avatar