1. Write a function that accepts an array of non-negative integers and returns the second largest integer in the array. Return -1 if there is no second largest.
{} =-1
July 9, 2010 at 10:35 AM
Hi Friend,
Try the following code:
import java.util.*;
public class SecondLargestNumber{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter 5 integers: ");
int secondlargest= -1;
int largest = -1;
int numbers[] =new int[5];
for (int i=0;i < numbers.length;i++){
numbers[i]=input.nextInt();
if(largest < numbers[i] ){
secondlargest = largest;
largest = numbers[i];
}
if(secondlargest < numbers[i] && largest != numbers[i] )
secondlargest = numbers[i];
}
System.out.println("Second Largest number is: "+secondlargest);
}
}
Thanks
Related Tutorials/Questions & Answers:
question1 - Java Beginnersquestion1 1. Write a function that accepts an array of non-negative integers and returns the second largest integer in the array. Return -1 if there is no second largest.
if the input array is return
{1, 2, 3, 4} =3
{{4
core java - Java Beginnerscore java
question1:- what is default members? Hi Friend,
If there is no access modifiers(public, private, protected etc) declared with any variable or method or class, then it means that there is a default
Advertisements
in project the questions to be displayed like this
test1
question1------[edit,delete]
test1
in projects the questions to be displayed like this,
test1
question1------[edit,delete
recursions - Java Beginnersrecursions Can somebody help me with these four questions. Please. i am learning recursions in Java.
Question1. Trying to find the value of result when its (5) from this code.
public int result(int n)
{
if (n==1
reduce complexity(request.getParameter("testid"));
int
question1 = Integer.parseInt(request...);
question.setQuestionId1(
question1);
question.setQuestionId2(question2
chn.ilp.test (request.getParameter("testid"));
int
question1 = Integer.parseInt(request...(
question1);
question.setQuestionId2(question2);
question.setQuestionId3