How to Find the Greatest Number in Array in Java

This is the simple java exercise that helps beginners to understand the array easily. The java program finds the largest number in a given array e.g. in array [2,15,11,55,8,10] = 55 will be the largest.

Source Code 

package newpackage.more;
import java.util.Scanner;

public class greatest {
public static void main(String[] args) {
int min=0; int max=0; int num;
System.out.println("enter the number count");
Scanner input=new Scanner(System.in); // java.util.Scanner
int n=input.nextInt();
System.out.println("");
num=input.nextInt();
min = num;
for(int i=1;i<n;i++)
{
num=input.nextInt();
if(num>max)
{
max=num;
}
if(num<min)
{
min=num;
}
}
System.out.println("");
System.out.println("Greatest:" + max);
}

}

Output of the Program

Find the Greatest Number in Array in Java


Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.

2 comments:

  1. Thanks for sharing this sample program. Here is the java program to find largest number in array.
    This java program used linear search algorithm in java to find largest and smallest number in array.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete