Sunday, September 4, 2016

Java Program To Find Largest and Smallest Number in an Array

  1. public class FindLargestSmallestNumber {
  2.  
  3.         public static void main(String[] args) {
  4.                
  5.                 //array of 10 numbers
  6.                 int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};
  7.                
  8.                 //assign first element of an array to largest and smallest
  9.                 int smallest = numbers[0];
  10.                 int largetst = numbers[0];
  11.                
  12.                 for(int i=1; i< numbers.length; i++)
  13.                 {
  14.                         if(numbers[i] > largetst)
  15.                                 largetst = numbers[i];
  16.                         else if (numbers[i] < smallest)
  17.                                 smallest = numbers[i];
  18.                        
  19.                 }
  20.                
  21.                 System.out.println("Largest Number is : " + largetst);
  22.                 System.out.println("Smallest Number is : " + smallest);
  23.         }
  24. }
  25.  
  26. /*
  27. Output of this program would be
  28. Largest Number is : 98
  29. Smallest Number is : 23
  30. */

Bhanu Namikaze

Author & Editor

Bhanu Namikaze is an Ethical Hacker, Passionate Blogger, Web Developer, Student and Mechanical Engineer. He Enjoys writing articles, Blogging, Solving Errors, Surfing and Social Networking. Feel Free To Contact Me @ Bhanu.

0 comments:

Post a Comment

Popular Posts

Copyrights @ Interview Questions.Me - Blogger Templates Designed by Templateism - Distributed By Protemplateslab

  • (91) 5544 654942
  • Bhanu@HackingDream.net
  • Interview Questions

Copyrights @ Interview Questions.Me -A Site By Bhanu In Association With Hacking Dream