Java Program to Find the Greatest Number in Array

This is the simple java code to find the greatest number in an array. There are many built-in functions available in java libraries to find the greatest number from array. But this code is working through a simple logic to find the greatest number in array.

Source Code 

package newpackage;
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");
Scanner input=new Scanner(System.in); // java.util.Scanner
int n=input.nextInt();
num=input.nextInt();
min = num;
for(int i=2;i<n;i++)
{
num=input.nextInt();
if(num>max)
{
max=num;
}
if(num<min)
{
min=num;
}
}
System.out.println("Greatest:" + max);
}
}
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.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment