Answer to Question #264555 in Java | JSP | JSF for Daniel

Question #264555

Hey I’ve got this cool idea for an app! It’s kind of simple but just hear me out. All the user has to do is enter a bunch of integers. Then the application prints the largest sum of a strictly ascending sequence of the array. A strictly ascending sequence is a sequence where the current number is always lesser than the next number. 


For example, the user enters 2 4 5 1 7 3, the output should be 11 (2 + 4 + 5).


Then that’s it! I think this is going to be a hit! Don’t you think? Well I do. If you help me then we’re going to be rich!


Note: For this problem, a sequence must contain at least 2 numbers. If there is no sequence found, then the largest sum is 0.


Output:

Enter the size: 6
Enter element #1: 2
Enter element #2: 4
Enter element #3: 5
Enter element #4: 1
Enter element #5: 7
Enter element #6: 3
Largest sum = 11
1
Expert's answer
2021-11-12T00:07:51-0500
import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
	    Scanner input=new Scanner(System.in);
	    int sum=0;
	    System.out.println("Enter the size");
	    int n=input.nextInt();
	    for(int i=1;i<=n;i++){
	      System.out.print("Enter element #:"+i+" ");
	      int el=input.nextInt();
	     if(el>(el-1)){
	          sum=i+i-1;
	      }
	      else{
	          System.out.println("Largest sum is 0");
	      }
	      
	    }
	     System.out.println("Largest sum is "+sum);
	    
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS