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



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

Have Programming Question? Ask it here!
 
 
 


MeeraDas
To find first two maximum numbers in an array
2 Answer(s)      2 years and 7 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: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 Tutorials/Questions & Answers:
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
Advertisements
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
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
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
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
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
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
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
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
Java array
Java array  Java program to find first two maximum numbers in an array,using single loop without sorting array
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
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 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
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
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
Java Find Automorphic numbers
Java Find Automorphic numbers In this section, you will learn how to find the automorphic numbers between 1 and 1000. Automorphic numbers are the numbers... of number 6 at the end. Here we are going to find the automorphic numbers between 1
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      
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
Add two big numbers - Java Beginners
Add two big numbers - Java Beginners  Hi, I am beginner in Java and leaned basic concepts of Java. Now I am trying to find example code for adding big numbers in Java. I need basic Java Beginners example. It should easy
Add two big numbers
Add two big numbers       In this section, you will learn how to add two big numbers. For adding two numbers implement two big decimal numbers then apply the Sum() method
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
mysql difference between two numbers
mysql difference between two numbers  How to get total bate difference between two dates for example 1/01/2012 and 1/02/2012 in MYSQL?   ... between two date. The syntax of DATEDIFF is .. SELECT DATEDIFF('2012-01-31 23:59
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
php two dimensional array
php two dimensional array  php two dimensional array example
adding two numbers with out using any operator
adding two numbers with out using any operator  how to add two numbers with out using any operator   import java.math.*; class AddNumbers { public static void main(String[] args) { BigInteger num1=new
adding two numbers using bitwise operators
adding two numbers using bitwise operators  adding two integer numbers with using bitwise opeators   Hi Friend, Try the following code: public class AddNumberUsingBitwiseOperators { public static void main(String
How to put text file numbers to array and check the position of numbers?
How to put text file numbers to array and check the position of numbers?   I have numbers in text file data.txt 12 9 8 3 1
two dimensional array
two dimensional array  how tow dimensional array works.How those loopes get incremented .and how every time the value of k changes
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
How to find the first 6 characters of string in regex?
How to find the first 6 characters of string in regex?  How to find the first 6 characters of string in regex. I know the Syntax... need the syntax to find the first 6 characters of string in regex using java
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
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 out last character of array
Find out last character of array  How we can find out the last character of array in java program
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
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 move curosr from one text field to another automatically when the first textfield reaches its maximum length
how to move curosr from one text field to another automatically when the first textfield reaches its maximum length  how to move curosr from one text field to another automatically when the first textfield reaches its maximum
JavaScript array of numbers
JavaScript array of numbers   ... in understanding 'Java Script array of numbers'. For this we use  Java Script language  as scripting language. We declare an array variable
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
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
array find all possible dimension
array find all possible dimension  Given array: int[] dimension = { 1, 2, 3 }; That produces the output: 1 2 3 12 13 23 123 I'm new to array...i can't figure out the way on even how to start... any idea?...really appreaciate
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. 
Advertisements
 

 

 

DMCA.com