mathajax

Java program Biggest number among N Integers

In order to find biggest number from unordered (not sorted) set of N numbers, a sequential search mechanism used, in which search starts from zero index to N-1 index.


A Biggest number from array numbers - Pseudocode

    Read a integer N

    initialize memory array of N element

    Read integers and store into an array
    
    set biggest = 0;

    for n=0  to N

      check array[n] > biggest  , set biggest = array[n]

    end for   
  
    print biggest
  

A Biggest number from array of numbers - Java program

The Java program read N integers from user via keyboard and stored on an array. It has a for-loop to start sequential search to find a biggest number from this array of numbers.

 

import java.io.IOException;
import java.util.Scanner;


public class BiggestAmongN 
{

 public static void main(String[] args) throws IOException 
  {
  
  Scanner sc =new Scanner(System.in);  
  System.out.println("Enter Number of Elements N");
  int N =sc.nextInt();      
  int array[] =new int[N];  
  
  for (int n=0;n<N;n++)
  {
   System.out.println("Enter " + n +  " Element ");        
          array[n] = sc.nextInt();
  }
  sc.close();
  
  int biggest=0;
  for (int n=0;n<N;n++)
  {
    if (   array[n] > biggest )
      biggest=array[n];
  }
  
 System.out.println("The Biggest value among N Elements is :
                                     " + biggest);
 }

}


 

Biggest Number - Java program Output


Enter Number of Elements N
5
Enter 0 Element 
20
Enter 1 Element 
38
Enter 2 Element 
43
Enter 3 Element 
67
Enter 4 Element 
16

The Biggest value among N Elements is : 67

Comments

Popular posts from this blog

Solving System of Linear Equations by Gauss Jordan Elimination

Matrix Forward and Back Substitution

Solve System of Linear Equations by LU Decompose

Chebyshev distance between two points

Binary 1's and 2's Complement