Home Answers Viewqa Java-Beginners To find first two maximum numbers in an array,using single loop without sorting array.

 
 


MeeraDas
To find first two maximum numbers in an array,using single loop without sorting array.
1 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);
    }
}









Related Pages:
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
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
Java array
Java array  Java program to find first two maximum numbers in an array,using single loop without sorting array
Array - Maximum
of an empty array. Loop direction Very rarely you will find a programmer who... that it is generally considered antisocial without a good reason (and it's not common to find... Java NotesArray - Maximum Finding the maximum is basically the same
Calculating the checksum of a Byte Array by using Adler32
Calculating the checksum of a Byte Array by using Adler32... of a Byte Array using Adler32. Adler32: It is a error checking technique used... field whose type is byte. To calculate the Checksum of the Byte Array by using
sorting
returnFirstNonRepeatedChar(char[] str) { //write an algorithm to find the first non.... Find the sub array with the largest sum. } please any body knows send this mail...) { //implement a search function for a sorted rotated array. //duplicates
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
Display tow dimensional array using one for loop in java
Display tow dimensional array using one for loop in java  Display tow dimensional array using one for loop in java
JavaScript Array
to understand in array first element example. For this we are using Java Scripting... into a single variable. For manipulating array objects and controlling their values we have... arrays to a single array with the use of concat() method.  
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
Bubble Sorting in Java
is an algorithm in which we are comparing first two values and put the larger one... by algorithm:-    In the first step compare first two values 12 and 9... Bubble Sorting in Java      
Java find prime numbers without using break statement
Java find prime numbers without using break statement In this tutorial, you will learn how to find the prime numbers without using break statement. You all are aware of Prime Numbers, these are the numbers which are either divided
one dimensional array using java
one dimensional array using java  design and implement a java program that will read a file containing numbers and compute the following statistics: the range(low,high) the average and the median
Finding duplicates using array in for loop
Finding duplicates using array in for loop  how to find the duplicates in array using for loop
Calculating the checksum of a Byte Array by using CRC32
of the Byte Array by using CRC32 we first need to create a class ChecksumByteArrayCRC32...Calculating the checksum of a Byte Array by using CRC32  ... of a Byte Array using CRC32. CRC32: It is a error checking technique used
odd numbers with loop
odd numbers with loop  get the odd numbers till 100 with for,while loop   Java find odd numbers: class OddNumbers { public static void main(String[] args) { for(int i=1;i<=100;i
php array loop count
php array loop count  Count the array using loop in php
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      
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
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
Merge two list into a single list
Description: This example demonstrate how to merge two lists into a single list. Here you find mainly three function. The insertnode function makes node... and the mergelist funtion merge the two lists into a single list. Code: # include
sorting numbers
sorting numbers  How to sort the numbers in ascending order   import java.util.*; class SortNumbers{ public static void main(String...=input.nextInt(); list.add(num); } System.out.println("Numbers
Generate array of random numbers without repetition
Generate array of random numbers without repetition In the previous section, you have learnt how to generate an array of random numbers from the given array... created an application that will generate the array of random numbers without
Array sorting - Java Beginners
Array sorting   Hello All. I need to sort one array based on the arrangement of another array. I fetch two arrays from somewhere... need to sort the "name" array alphabetically. I can do that easily using
java array sorting manual way
java array sorting manual way  Hi, Anyone has any sample code on how to sort a array manually without using the inbuild sorting algorithm? Thanks
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
sorting - Java Beginners
; Easy Sorting: For all the values in the array A, find the largest and store.... In the next repetition of the loop, find the largest value in A again and store the value... readIntegers() that o reads the user inputs o stores them in an array, and o
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
Binary Search in Java
an array using Binary Search. The advantage of a binary search over a linear search is astounding for large numbers. It can be done either recursively or iteratively. In the given code, we have allowed the user to enter the numbers
PHP array length for, PHP array length for loop
In this tutorial we will iterate PHP array using the for loop. We will find the length of the array and then print all the values in it. The following PHP code shows how you can get the array length and then use the for loop
for loop
for loop  using only two for loop how can print below numbers 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 7 7 7 7 7 7 7 6 6 6 6 6 6 5 5 5 5
for loop
for loop  using only two for loop how can print below numbers 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 7 7 7 7 7 7 7 6 6 6 6 6 6 5 5 5 5
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
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. 
sorting
sorting  how to do sorting without using bubble sort,selection sort
JavaScript array sort numerically
;    As in our previous example of sorting array elements... in this example of sorting array elements ( numeric elements ) we have created... need to define a sorting function for sorting numbers. Given below is the full
Swap two numbers without using third variable
Swap two numbers without using third variable In this section we are going to swap two variables without using the third variable. For this, we have used... from the command prompt. Instead of using temporary variable, we have done some
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
Read text file to 2D array and sorting the second column
Read text file to 2D array and sorting the second column  we found.... when exams conducted fast 4 times.The first column is the student name and the second column is marks in tests.What we need to find is which student marks
Two dimensional array in java
element. To process the two-dimension array, for loop is used . Example...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
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
C Array default values
in Array. The example below contains an array arr of integer type. The maximum size of the array is 4. We have allocated only two values in the array like this:  ...;of an array to a default value. We can do this with a for loop that sets 
Insertion, sorting and searching in array
; } Insertion, sorting and searching in array   ... to perform searching and sorting in array. In the java code given below we have declare an array of integer type and then sort by using java.util.Arrays.Sort
PHP Array Random Sorting
PHP Array Random Sort PHP provides another kind of sorting which is called Random Sorting,  in PHP shuffle() is used to sort in this manner. This function shuffles an array. Small description of shuffle function is as follows
Dividing Element of Two Dimensional Array
with us we need to divided both matrix. This program using the two for loop. So... Dividing  Element of Two Dimensional Array... divide of two matrix. First all to we have to define class "
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