Array Loop Practice

Collapse Content

The last node discussed the code to find the largest number in an array. Now see if you can modify it slightly to find the smallest number in an array.

More Practice
After you solve this challenge, see if you can solve Harder Array Loop Practice. You can also try solve some of the following Array Loop challenges:


You will be given ar, an array of numbers as input. Find and print the smallest number in ar.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.


  • There is probably better ways but I worked out the largest number first.

  • static void doStuff(int[] ar){
    //your code here
    int smallest=0;
    for(int i=0;i<ar.length;i++){
    return System.out.print(smallest);

  • Hello! Don't understand the error...any help would be apreciated!

  • Can you help me understand what wrong I have done:
    static void doStuff(int[] ar){
    //your code here
    int smallest=ar[0];
    for(int i=0;i<ar.length;i++){
    if(ar[i] < smallest){
    smallest =ar[i];

  • @mohammad, it looks like your print statement is outside the method, which doesn't work in Java. move it up to before the } so it runs when the for-loop is finsihed.

  • I did as you said... the code work in eclipse but not in above editor...

  • @mohammad, it looks like there are some other errors in your code also. See if you can modify the code from the previous page to find the smallest number instead of the largest.

  • i cant understand what its wrong here, i plug the code of the last page and modify and the output still doesnt make any sense.
    'static void doStuff(int[] ar){

  • i found the answer. There is a mistake in the boilerplate code, under 'doStuff(ar);' you have to put the line 'System.out.println("");'. Then the code should work.

  • @Peter, thanks. I mentioned in the question to print each number on its own line, so that if you use System.out.println (with the ln for line) it works, and you don't need to edit the boilerplate

  • int smallest = ar[0];
    for(int i=0; i < ar.length; i = i +1){

            if(ar[i] < smallest){
                smallest = ar[i];
  • Small suggestion (however this may only be me), after reading the instructions it says "(Print each number on its own line.)", i think this is misleading or requires further clarification.

    Because first it is said to print the smallest number but then print each number...

  • int valor = ar[0];
    for(int i = 0; i<= ar.length - 1; i = i + 1){
    if (valor <= ar[i]){
    else {
    valor = ar[i];

  • Hello, I keep getting the correct outputs, but with .0 on the end. What am I doing wrong?
    import java.util.*;
    class Main{
    static void doStuff(int[] ar){

  • @TyB, use ints instead of doubles.

  • Thank you!

  • private static void doStuff(int[] ar)
  • ' static void doStuff(int[] ar){
    int small=ar[0];
    for (int i=0; iar[i]){

  • I used this strategy: static void doStuff(int[] ar){
    int smallest = 0;
    int teller = 0;
    for(int i = 0; i < ar.length; i = i+1){
    if(teller == 0){

  • im getting errors like error: ';' expected
    public static void main(String[] args) {

  • what does this mean?
    cannot return a value from method whose result type is void
    return smallest;

  • another solution using for each:
    int tmp = ar[0];
    for (int element : ar)
    if (element < tmp ) tmp = element;

        System.out.println (tmp);
  • ugghh had to cheat on this one ....will do better next time

Contact Us
Sign in or email us at [email protected]