-2

I'm new to programming. I want to find the maximum and minimum number in a given array. So I wrote this code. Although this gives the correct minimum number, it gives multiple maximum numbers. Can someone help me with this?

package maximumandminimum;

public class Maximumandminimum {

public static void main(String[] args) 
{
  int arr[] = {33,55,80,90,12,56,78};

          int min = arr[0];
          int max = arr[0];

          for(int i = 0; i< arr.length;i++)
          {
              if(arr[i]<min){
                  min= arr[i];
                  System.out.println("minumum number is"+min);
              }
              if(arr[i]>max){
               max = arr[i];
               System.out.println("maximum number is"+max);

          }

          }
}

}

  • 2
    Print **after** your loop (not **in** your loop). **Never** post code as an image. **Edit** your question to include the **text** of the code. – Elliott Frisch Mar 31 '20 at 15:24
  • 3
    I'm pretty sure that the best person to help you here is _you_: this is not a complicated question, it's elementary programming with far too many answers easily found by searching already. With that said: remember to read through [how to ask a good question](/help/how-to-ask) and follow the policies it sets out by editing your post to match. – Mike 'Pomax' Kamermans Mar 31 '20 at 15:24
  • Elliott Frisch Thank you for the advice. – sachintha imindhu Mar 31 '20 at 16:08

1 Answers1

1

This has been solved here: Finding the max/min value in an array of primitives using Java

Don't forget to print outside the loop (to get one answer).

ike5
  • 188
  • 1
  • 10