Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

Have Programming Question? Ask it here!
 
 
 


MeeraDas
To find first two maximum numbers in an array,using single loop without sorting array.
1 Answer(s)      2 years and 8 months ago
Posted in : Java Beginners


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


Advertisement
View Answers

December 27, 2011 at 12:12 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 Tutorials/Questions & Answers:
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
Advertisements
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
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
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
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
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
php array loop count
php array loop count  Count the array using loop in php
Swapping of two numbers without using third variable
Swapping of two numbers without using third variable In this tutorial we will learn about swapping of two number in java without using third variable... to swap two variables without using the third variables. First we declare one
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
Finding duplicates using array in for loop
Finding duplicates using array in for loop  how to find the duplicates in array using for loop
Sorting the array
Sorting the array  Implement a program to process votes for 5 candidates in a talent contest. The program should use a String array to hold the names of the 5 candidates and an integer array to record the number of votes for each
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
create a string from an array using JavaScript
create a string from an array using JavaScript  How to use "join()" to create a string from an array using JavaScript
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
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
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
Addition of two numbers
Addition of two numbers  addition of two numbers
Sorting arraylist without using Collection.sort()
Sorting arraylist without using Collection.sort()  Hi, How can I sort an arraylist without using Collection.sort() nad also I am not allowed to use any other data structure in the program? Is there any algorithm by using which I
Listing all even numbers between two numbers
Listing all even numbers between two numbers  Hi, How to write code to list all the even numbers between two given numbers? Thanks   Hi, You function will take 2 numbers and the find the even numbers between
sorting array in java
sorting array in java  How to sort array in Java or JavaScript?   JavaScript Sorting array tutorial   Java Sort array of strings...[] args) { String array[]=new String[5]; Scanner input = new Scanner
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
Adding two numbers
Adding two numbers  Accepting value ffrom the keyboard and adding two numbers
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
Array Sorting - Development process
Array Sorting  Hello sir, Now i m doing various Array sorting(Bubble,insertion,Quick,merge etc ) I want to display Array Sort in steps by step like... is the code to display array sorting with moving graphics(color)in steps
What are the different functions in sorting an array?
What are the different functions in sorting an array?  What are the different functions in sorting an array
Iteration without loop
Iteration without loop  Hi, Is there any way I can have a line of code iterate a number of times set by the user without having a while loop or any other control statements? Thanks!   import java.util.*; class
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
A single dimensional array is called
A single dimensional array is called  What is a single dimensional array called?   A single dimensional array is called LIST
Swapping of two numbers in java
Swapping of two numbers in java In this example we are going to describe swapping of two numbers in java  without using the third number in java. We... values from the command prompt. The swapping of two numbers is based on simple
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
Convert a string representation of a hex dump to a byte array using Java?
Convert a string representation of a hex dump to a byte array using Java?  Convert a string representation of a hex dump to a byte array using Java
Two Dimensional Array Program Using Nested For Loop
Two Dimensional Array Program Using Nested For Loop ...;. We are going to make a integer for array declaration Two dimensional array program. We are going to using for loop for performing the five rows and five
php array loop while
php array loop while  Using the php array while loop
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
php array loop foreach
php array loop foreach  Display the php array elements in foreach loop
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
sorting an array of string with duplicate values - Java Beginners
sorting an array of string  Example to sort array string
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
C calculate sum of first and last element of array
enters the number. Then using the index of the array, we have found first...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
How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... for employe class and write for below sample code ,in this code i can findout maximum
How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... for employe class and write for below sample code ,in this code i can findout maximum
 

 

 

DMCA.com