Basic Java Examples

Find Largest and Smallest Number in an Array Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

28 Comments

  • class TestArray
    {
    public static void main(String args[])
    {
    int arr[]={14,5,12,9,8};
    Arr a1=new Arr(arr);
    int m=a1.minArray();
    System.out.println(“The minimum no. is: “+m);
    a1.Display();
    }//end main
    }//class end

    class Arr
    {
    private int A[];
    Arr(int ar[])
    {
    A=new int[ar.length];
    for(int i=0;i>ar.length;i++)
    {
    A[i]=ar[i];
    }
    }
    int minArray()
    {
    int min;
    min=A[0];
    for(int v:A)
    {
    if(min>v)
    min=v;
    } return min;

    }//end of minArray

    void Display()
    {
    System.out.println(” “);

    }// end of display

    }//end class

    can u please check this program

      • yes..
        you can do it like…
        1-find the odd numbers… ex- between 1 to 10
        then u will get the odd numbers .. then just compare the numbers with each other ..just like normal comparison..
        just like we do in finding the largest of three numbers..
        surely u will get your ans…

  • can you short out the error and notifie mee
    please

    class TestArray
    {
    public static void main(String args[])
    {
    int arr[]={14,5,12,9,8};
    Arr a1=new Arr(arr);
    int m=a1.minArray();
    System.out.println(“The minimum no. is: “+m);
    a1.Display();
    }//end main
    }//class end

    class Arr
    {
    private int A[];
    Arr(int ar[])
    {
    A=new int[ar.length];
    for(int i=0;i>ar.length;i++)
    {
    A[i]=ar[i];
    }
    }
    int minArray()
    {
    int min;
    min=A[0];
    for(int i=0;i<a.length;i++) {=”” if(min=””>A[i])
    min=A[i];
    } return min;

    }//end of minArray

    void Display()
    {
    System.out.println(” “);

    }// end of display

    }//end class

  • why do we have to write “new int[]”

    int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};

    if without new int[] works too? what’s the difference?

    • Hi Kat Magcaling,
      When we write the “new” keyword we are creating and initializing the array at once.
      Without writing the “new” keyword the array is not created , if u simply write int[]number; then it means that the array has only been defined.And without the initialization you cannot direct the compiler !!! Hope i was helpful 🙂

      • @twitter-111547595:disqus

        It does not make any difference
        int array[]={1,2,3,4,5,5,6);
        will initialise the elements of the array

    • its declared like this becoz… once we declare array then we have to
      create memory block where we can store those numbers.. but jvm does it
      to create memory block for this array numbers … we required to
      initialize those number to help jvm to create the new memory blocks to
      store our numbers which we will going to enter in the array… hope u
      understand this…. 🙂

  • can anyone help me? i need the coding where users can enter 5 different numbers and the system will display the maximum number entered by user. the coding above is quite what i needed but the coding above already set the numbers.

    • import java.util.*;
      public class MinMaxNumber
      {

      public static void main(String[] args)
      {
      Scanner scan = new Scanner(System.in);
      System.out.println(“Enter how many numbers you want to input”);
      int size = scan.nextInt();
      int[] input = new int[size];
      System.out.println(“Enter the numbers”);
      for(int i=0;i<size;i++) {=”” input[i]=”scan.nextInt();” }=”” arrays.sort(input);=”” system.out.println(“max=”” number:=”” “+input[size-1]);=”” system.out.println(“max=”” number:=”” “+input[0]);=”” }=”” }=””>

  • hi can anybody help me? i need to create a program which user can enter 5 different numbers and the system will display the largest number.

    • for(int i=o;i<=10;i++)
      {
      for(int j=1;j<=a[].length();j++)
      {
      if(a[i]<=a[j])
      {
      j++;
      }
      elseif(a[i]>a[j])
      {
      int temp;
      temp=a[j];
      a[i]=a[j];
      a[j]=temp;
      i++;
      j++;
      }
      system.out.println(“after sorting”a[i]);
      system.out.println(“largest number is”a[n-1]);
      }
      }

    • i can provide you with the sol
      this is very simply first sort entire array.after that just print a[n-1]….u will get it
      for(int i=o;i<=10;i++)
      {
      for(int j=1;j<=a[].length();j++)
      {
      if(a[i]<=a[j])
      {
      j++;
      }
      elseif(a[i]>a[j])
      {
      int temp;
      temp=a[j];
      a[i]=a[j];
      a[j]=temp;
      i++;
      j++;
      }
      system.out.println(“after sorting”a[i]);
      system.out.println(“largest number is”a[n-1]);
      }
      }
      thank you

  • one required application is a program tha calculates customer bills.This month cars are being rented for $35 per day with 9% tax applied 1;use divide and conquer strategy to devise an algorithm

  • i thought arrays start at 0 so if i = i then it will start with numbers[1] which is 43, the second number in the array. does how array works change with java version or something?

  • just do this:
    int[] list = new int[]{2, 3};
    int maxNum = 0;
    for(int i = 1; i < list.length; i++)
    {
    if(list[maxNum] < list[i])
    {
    maxNum = i;
    }
    System.out.println(list[maxNum]);
    }

  • suppose I have an array
    int arr[]={1,3,2,1,3,4,5,,6,1,7,5,8,1,15,13,12} and how to replace 1 by sum of all even nos square.
    like 1 replace by (2*2)+(4*4)+(6*6)+(8*8)+(12*12) for the 1st 1 then after next 1 replace by (4*4)+(6*6)+(8*8)+(12*12) just like that

    • hi shrivastava,
      actually i brought a solution for your question.but,i am not sure that it will give u expected output but my logic is correct u can find a solution here.
      firstly.
      create an array
      next pickup even numbers from the list,also count no of 1’s.
      find positions for every 1.
      code:
      public class xyz
      {
      public static void main(String args[])
      {
      scanner sc=new scanner(System.in);
      sop(“enter elements into array”);
      public int a[]=sc.nextint();
      for(int i=0;i<=a[].length();i++)
      {
      if(a[i]/2==0) //detecting even series
      {int b[]=a[i];
      sop(“elements are”+b[i]);
      }
      elseif(a[i]==1) //finding position number and counting no of 1’s
      { count=0;
      sop(“position of 1 in array is”+i);
      int c[]=i;
      count++;
      sop(“count is “+count);
      }

      for(int j=0;j<=count;j++)
      {
      for(int k=j;k<=b[].length();k++)
      { int y,z;
      sop(“elements are”,b[k]);
      y=2*b[k]((b[k]+1))((2*b[k])+1)/3;// expresstion is ” 2n(n+1)(2n+1)/3 “
      z=c[k];
      a[z]=y;// logic and replacing
      }
      }
      sop(“after replacing series is”+a[i]);
      }
      }}
      here sop:system.out.println
      thank you

  • // finding the lowest and highest value using multidimensional array I.O. Loops
    import java.io.*;

    public class MultiDimensionalArray {

    public static void main(String[] args)throws IOException {

    BufferedReader c= new BufferedReader (new InputStreamReader (System.in) );

    System.out.print ( “Enter Number Column : ” );

    int column = Integer.parseInt(c.readLine());

    System.out.print ( “Enter Number Row : ” );

    int row = Integer.parseInt(c.readLine());

    int array [][] = new int [column][row];

    int max = array [0][0];

    int min = array [0][0];

    for ( int i=0 ; i < array.length; i++){

    for (int j=0 ; j<array[i].length; j++){=”” system.out.print(“enter=”” array=”” values=”” [“+i+”][“+j+”]:=”” “=”” );=”” array[i][j]=”Integer.parseInt” (c.readline());=”” system.out.print(“\n”);=”” if(array[i][j]=””> max){

    max = array[i][j];

    }

    if(array[i][j] < min){

    min = array[i][j];

    }

    }

    }

    System.out.println(“The Min Number :”+ min);

    System.out.println(“The Max Number :”+ max);

    }

    }

  • package MyPrograms;

    import java.util.Scanner;

    public class LargestUser {

    public static void main(String[] args)
    {
    // TODO Auto-generated method stub
    int num=0;
    int num1=0;
    Scanner number=new Scanner(“System.in”);
    System.out.println(“Enter a number: “);
    for(int i=1;i<4;i++)
    {
    num=number.nextInt();
    if(num>num1)
    {
    num1=num;
    }
    }
    System.out.println(num1+ ” is the largest number”);

    }
    Exception in thread “main” java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at MyPrograms.LargestUser.main(LargestUser.java:16)

    Can any one say whats wrong in my code?
    I tried to find the largest number

Sponsors

Facebook Fans