Home Answers Viewqa Java-Beginners To find first two maximum numbers in an array

 
 


MeeraDas
To find first two maximum numbers in an array
2 Answer(s)      a year and 6 months ago
Posted in : Java Beginners

Java program to find first two maximum numbers in an array,using single loop without sorting array.

View Answers

December 27, 2011 at 12:11 PM


import java.util.*;

class ArrayExample {
    public static void main(String[] args) {
        int secondlargest = 0;
        int largest = 0;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter array values: ");
        int arr[] = new int[5];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = input.nextInt();
            if (largest < arr[i]) {
                secondlargest = largest;
                largest = arr[i];
            }
            if (secondlargest < arr[i] && largest != arr[i])
                secondlargest = arr[i];
        }
        System.out.println("Largest number is: " + largest);
        System.out.println("Second Largest number is: " + secondlargest);
    }
}

September 10, 2012 at 2:36 PM


package com.java.interview.array;

import java.util.Scanner;

public class TwoLargestElementsOfArray {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    int secondlargest = 0;
    int largest = 0;
    int size = 0;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the size of array: ");
    size = input.nextInt();
    int arr[] = new int[size];
    if (size < 2)
        System.out.println("Please enter the valid size of array");
    System.out.println("Enter array values: ");
    for (int i = 0; i < arr.length; i++) {
        arr[i] = input.nextInt();
        if (largest < arr[i]) {
            secondlargest = largest;
            largest = arr[i];
        }
        if (secondlargest < arr[i] && largest != arr[i])
            secondlargest = arr[i];
    }
    System.out.println("Largest number is: " + largest);
    System.out.println("Second Largest number is: " + secondlargest);

}

}









Related Pages:
To find first two maximum numbers in an array
To find first two maximum numbers in an array  Java program to find first two maximum numbers in an array,using single loop without sorting array
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
Numbers
Java NotesNumbers Two kinds of numbers. There are basically two kinds of numbers in Java and most other programming languages: binary integers (most commonly using the type int) and binary floating-point numbers (most commonly using
Array - Maximum
of an empty array. Loop direction Very rarely you will find a programmer who... Java NotesArray - Maximum Finding the maximum is basically the same as finding the minimum; remember the first value, then look through all the other
Java array
Java array  Java program to find first two maximum numbers in an array,using single loop without sorting array
Comparing Two Numbers
of comparing two numbers and finding out the greater one. First of all, name a class "Comparing" and take two numbers in this class. Here we have taken a=24... Comparing Two Numbers      
Two Dimensional array program
Two Dimensional array program  consider a two dimensional array... to elements of the array such that the last element become the first one and the first become the last.let the program output elements of the first array
Write a program to list all even numbers between two numbers
all the even numbers between two numbers. For this first create a class named... Write a program to list all even numbers between two numbers...; in the System method.  As we have to find out all the even numbers
Applet for add two numbers
); add(text2); label3 = new Label("Sum of Two Numbers...Applet for add two numbers  what is the java applet code for add two numbers?   import java.awt.Graphics; import javax.swing.*; public
Find L.C.M. of two numbers in Java
Find L.C.M. of two numbers in Java Program In this section, you will learn how to find LCM(Least Common Element) of two integers.The least common multiple is the smallest positive integer that is a multiple of both the numbers. Here we
adding of two numbers in designing of frame
adding of two numbers in designing of frame  hello sir, now i'm create two textfield for mark1&mark2 from db.how to add these two numbers... JFrame(); JLabel label1=new JLabel("First Number: "); JLabel label2=new
JavaScript determine add, product of two numbers
numbers. To find this, we have created two textboxes and four button...JavaScript determine add, product of two numbers In this section, you...("Sum of two numbers is: "+sum); } function divide
Java Find Median
of numbers. There is no built in method to find the median of a list of numbers... numbers. Here we have created an example that stores 5 integers in an array... the median from the set of numbers stored in an array. Example: import java.util.
adding two numbers - Java Beginners
adding two numbers  hii friends....... this is my program... InputStreamReader(System.in)); System.out.println("Enter first no.:"); x[1...]=Integer.parseInt(in.readLine()); int s=x[1]+y[1]; System.out.println("Sum of two
generate random numbers and display the largest
it. Write a method to find the largest value in an array. Write a program that takes... to the screen and uses your method to find the largest value in the array, then prints...generate random numbers and display the largest  Hi, I am using
one dimensional array program
: - The first number in the input file will be the number of numbers in the file The maximum number of numbers in the file will be 1000 To find the median, you must first sort the numbers The range of numbers is: -44.... to 7654
maximum size of arraylist - java
maximum size of arraylist - java  1.what is the maximum size... on size of RAM. The theoretical maximum number of elements in an ArrayList is 2^31-1. 2)Inserting an element in the middle of the array list and removing
Prime Numbers
to enter a positive integer n, and which then creates and populates an int array with the first n prime numbers. Your program should then display the contents of the array elements. You program should also ensure that the user enters
Find Twin Primes Number using Java
Find Twin Primes Number using Java In this section, you will learn how find primes and twin primes from the first 100 natural numbers. For this, we have used... are the pair of prime numbers which are having a difference of 2 between two
Add Two Numbers in Java
Add Two Numbers in Java     ... of two numbers! Sum: 32... these arguments and print the addition of those numbers. In this example, args
Swapping of two numbers
swap the numbers. To swap two numbers first we have to declare a class Swapping... Swapping of two numbers      ... program to calculate swap of two numbers. Swapping is used where  you want
Sum of first n numbers
Sum of first n numbers  i want a simple java program which will show the sum of first n numbers....   import java.util.*; public class...; } System.out.println("Sum of Numbers from 1 to "+n+" : "+sum
Addition of two numbers
Addition of two numbers  addition of two numbers
this code gives addition of 2 numbers in j2me..but the code is not executing ..can u pls find out the error...??
=new Form("Addtion of two numbers"); // result=new Command("Result...this code gives addition of 2 numbers in j2me..but the code is not executing ..can u pls find out the error...??  import javax.microedition.midlet.
C calculate sum of first and last element of array
C calculate sum of first and last element of array In this section, you will learn how to calculate sum of first and last element from the array of five... numbers of their choice. These numbers will get stored into the array as the user
Adding two numbers
Adding two numbers  Accepting value ffrom the keyboard and adding two numbers
Find sum of all the elements of matrix using Java
Find sum of all the elements of matrix using Java A matrix is a rectangular array of numbers. The numbers in the matrix are called its entries or its elements... and the elements they wish to be in the matrix. These elements are stored into the two
Find Numbers which are palindrome and prime
Find Numbers which are palindrome and prime In this section, you will learn how to find the numbers from 1 to 500 which are palindrome and prime. To compute this, we have created two methods isPalindrome() of ArrayList type and isPrime
How to find maximum value in ArrayList
How to find maximum value in ArrayList  **Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object
How to find maximum value in ArrayList
How to find maximum value in ArrayList  **Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object
How to find maximum value in ArrayList
How to find maximum value in ArrayList  **Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object
How to find maximum value in ArrayList
How to find maximum value in ArrayList  **Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object
Two- Dimensional Array - Java Beginners
Two- Dimensional Array  I am new in java programming. I am creating a two-dimensional array. This is my code ** class BinaryNumbers { public static void main(String[] args) { //create a two-dimensional array int ROWS = 21
numbers divisible by 5 and 6
numbers divisible by 5 and 6  Find the first ten numbers which are greater than Long.MAX_VALUE divisible by 5 and 6
generating random numbers - Java Beginners
of the known prices (except for the first 5, which don't have enough predecessors). We...*60 = 60). So the errors are 10 and 0 for the two predictions with an average...{ public static double avgError(double []array){ double aa[]=new double[5
Two Dimensional Array Program
Two Dimensional Array Program Using Nested for loop...; program . In this session we will teach how to use of a two dimensional array... are going to make two dimensional array having three row and three columns. 
Array
Array  Write a program to store the population of 8 countries. i) Define two arrays to store the names of the countries and their populationââ?¬â?¢s numbers. ii) Write a loop that uses these arrays to print each countryââ?¬â?¢s
search in Two Tables to Find data and view by jsp
search in Two Tables to Find data and view by jsp  hi i want search in Two Tables to Find data and view by jsp <%@page import... (create two table the first table: the bills for user the admin insert
Two dimensional array in java
Two dimensional array in java. In this section you will learn about two... will be established, it is fixed when created.In two dimension array data...: To declare an array just two square bracket with empty list required as follows
find the first character is vowel or not
find the first character is vowel or not  hi friend, can u plz send me javascript code for find the wether the first character is a vowel or not. 2) check wether the last character is same as the first character or not 3) check
JavaScript array position
by which we can find the position of the array item therefore we have created a method getPosition() which takes two arguments: first is the array object...;  In this section of JavaScript array tutorial we have to find
Random numbers - Introduction
to maximum), and how they are distributed. There are two common types of distributions... Java NotesRandom numbers - Introduction When to use random numbers There are many types of programs that use random numbers. Game programs use them
how do i begin a two dimensional array?
how do i begin a two dimensional array?  I'm new to java programming and need to create a two dimensional array that enters exactly what is entered in the first dimension and then the first non-white space character of what
USE ARRAY TO FIND THE LARGEST NUMBER AND OCCURRENCE
USE ARRAY TO FIND THE LARGEST NUMBER AND OCCURRENCE  i have some exercise from my lecturer and I still can't understand about ARRAY. here it is: write a program that reads 10 numbers from the keyboard. find the largest number
Dividing Element of Two Dimensional Array
Dividing  Element of Two Dimensional Array... divide of two matrix. First all to we have to define class "ArrayDivideMatrix". Here you will learn how to use two matrix array for developing
How to show the maximum and the minimum in array...
How to show the maximum and the minimum in array...  I have a bookstore application, but I cannot show the maximum and the minimum price of the book. Please see my codes below. import java.util.*; public class Book{ public
How to show the maximum and the minimum in array...
How to show the maximum and the minimum in array...  I have a bookstore application, but I cannot show the maximum and the minimum price of the book. Please see my codes below. import java.util.*; public class Book{ public
Find a missing element from array
Find a missing element from array We have two arrays A and B where B consists... the array B. From these two arrays, we have to find the missing element from the second array i.e from B. For this, we have calculated the sum of elements from both
Midpoint of two number
important to find the number that is exactly between two numbers. In this example we are finding a midpoint of two numbers by using the while loop.   ...Midpoint of two number