Sample program to find largest number in java

Java
class Numbers
{
public static void main(String args[])
{
int i,great_no,small_no;
/*
//Alternate code to input numbers from commandline.
int number1; // first number to add
int number2; // second number to add
int sum; // sum of number1 and number2
Scanner input = new Scanner( System.in );
System.out.print( "Enter first integer: " ); // prompt
number1 = input.nextInt(); // read first number from user

System.out.print( "Enter second integer: " ); // prompt
number2 = input.nextInt(); // read second number from user
*/
int no[] = {5,78,1,6,74,9,6,4,6,7};
System.out.println("Elements in the Array : ");
for(i=0;i< 10;i++)
System.out.print(no[i]+"\t");
great_no = no[0]; small_no = no[0];
for(i=1;i< 10;i++)
{
if(great_no< no[i])
great_no = no[i];
else if(small_no>no[i])
small_no = no[i];
}
System.out.print("\nGreatest Number : "+great_no);
System.out.print("\nSmallest Number : "+small_no);
}
}

/************** OUTPUT ****************

Elements in the Array :
5 78 1 6 74 9 6 4 6 7

Greatest Number : 78
Smallest Number : 1 */ 

In case of any ┬ęCopyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply