Click here to Skip to main content
15,890,946 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Take an array of user defined size and input all values from user that print sum of 1st and last element , sum of 2nd and 2nd last element , sum of 3rd and 3rd last element and so on if array has even size and if array size is odd than print the middle value as it is.using a loop. I need a code to this specific problem in java?

What I have tried:

Java
package try5;

import java.util.Scanner;

public class Try5 {

    public static void main(String[] args) {
    int length,sum = 0;
    Scanner  num= new Scanner(System.in);
    
    System.out.println("Enter the total numbers in an array:");
    length = num.nextInt();
    
    int[] array = new int[length];
    for(int i=0; i<length; i++){
        System.out.println("Values::");
        array[i] = num.nextInt();
    }
    
        for(int h=0; h<length; h++){
            
        for(int t=1; t>h; t--){
        sum = array[h]+array[length-t];
        System.out.println("sum:" +sum);
        sum = array[h+1]+array[length-t-1];
        System.out.println("sum:" +sum);}   
    }
}
}
Posted
Updated 4-Sep-17 1:26am
v2

You don't need two loops. Just use one while loop with two variables:
Java
int first = 0;
int last = length - 1;
while (first <= last)
{
    if (first < last)
        System.out.printf("%d. sum: %d%n", first + 1, array[first] + array[last]);
    else
        System.out.printf("%d. mid: %d%n", first + 1, array[first]);
    first++;
    last--;
}

You might also use a for loop instead (iterating over the half length) and calculate the last index inside the loop:
Java
for (int first = 0; first < (length + 1) / 2; first++)
{
    int last = length - 1 - first;
    if (first < last)
        System.out.printf("%d. sum: %d%n", first + 1, array[first] + array[last]);
    else
        System.out.printf("%d. mid: %d%n", first + 1, array[first]);
}
 
Share this answer
 
Quote:
I need a code to this specific problem in java?

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
Member 13390616 4-Sep-17 7:21am    
Where do I supposed to insert if else statement?
Why second loop is not working? it just takes the sum once.
How do we access the middle number?
Please respond!
OriginalGriff 4-Sep-17 8:21am    
What happens when you use the debugger to see what is going on?
What did it show you?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900