# Array Loop Practice

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:

### Challenge

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

Alternatively, you can try out Learneroo before signing up.

• #### niveoserenity

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

• #### Edison Lao

static void doStuff(int[] ar){
int smallest=0;
for(int i=0;i<ar.length;i++){
if(ar[i]<smallest){
smallest=ar[i];
}
}
return System.out.print(smallest);

• #### Fábio Teles

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){
int smallest=ar;
for(int i=0;i<ar.length;i++){
if(ar[i] < smallest){
smallest =ar[i];
}

``````    }
System.out.print(smallest);
}
``````
• #### Learneroo

@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...

• #### Learneroo

@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.

• #### Carlos Díaz Ramirez

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){

• #### Peter Lacres

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.

• #### Learneroo

@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

• #### Cliff Karlsson

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

``````        if(ar[i] < smallest){
smallest = ar[i];
}

}
System.out.println(smallest);
``````
• #### David

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...

• #### isnard

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

• #### Ty B

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){

• #### Learneroo

@TyB, use ints instead of doubles.

• #### Ty B

Thank you!

• #### 許友誠

``````private static void doStuff(int[] ar)
{
Arrays.sort(ar);
System.out.println(ar);
}
``````
• #### Michał Targiel

' static void doStuff(int[] ar){
int small=ar;
for (int i=0; iar[i]){
small=ar[i];
}
}
System.out.print(small);
}'

• #### Olga

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){

• #### paul fothergill

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

• #### John

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

• #### flowra

another solution using for each:
int tmp = ar;
for (int element : ar)
{
if (element < tmp ) tmp = element;
}

``````    System.out.println (tmp);
``````
• #### Bernard Mitchell

ugghh had to cheat on this one ....will do better next time