Sunday, July 19, 2015

Second Highest Number in Java

In this simple program it will ask the user to enter five numbers and then our program will check and determine which of the five number has the second highest value.

I hope you will find my work useful and beneficial. If you have some questions about programming, about my work please send me an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com. People here in the Philippines can contact me at  my mobile number 09173084360.

Thank you very much and Happy Programming.

Sample Program Output

Program Listing

// finding_second.java
// Programmer : Mr. Jake R. Pomperada, MAED-IT
// Date       : July 19, 2015
// Tools      : Netbeans 8.0.2
// Email      : jakerpomperada@yahoo.com and jakerpomperada@gmail.com

// Write a program in Java using one dimensional array that will ask the user
// to give five numbers and then the program will determine which of the five
// number is the second to the highest number.

import java.util.Scanner;
 
public class finding_second {
    
    public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      
     char ch;   
 do {
    int values[]=new int[5];
    int highest = Integer.MIN_VALUE;
    int highest2nd = Integer.MIN_VALUE;
    
     System.out.println();
      System.out.print("\t FINDING SECOND HIGHEST NUMBER");
      System.out.println("\n");
    for(int i=0;i<5;i++){
        int x=(i+1);
         System.out.print("Enter Value Number " + x + ": ");
          values[i]=input.nextInt();

    }
    for(int i=0;i<5;i++){
        if (values[i]>=highest) { 
            highest2nd = highest;
            highest = values[i];
        } else if (values[i]>= highest2nd)
            highest2nd =values[i];
    }
    System.out.println();
    System.out.println("The second highest number is " + highest2nd +"."); 
     System.out.print("\nDo you want to continue (Type y or n) : ");
      ch = input.next().charAt(0);                        
     } while (ch == 'Y'|| ch == 'y');  
      System.out.println();
      System.out.print("\t THANK YOU FOR USING THIS PROGRAM");
      System.out.println("\n\n");
    }
}


No comments:

Post a Comment