0

i am working on a program to add numbers using an array. I have completed a lot of it but am troubled at the last part adding the actual numbers in the code. Here is my code.

public static void main (String[] args) {
    Scanner input= new Scanner(System.in);
    System.out.println("Enter size of array");
    int n= input.nextInt();
    int[] x= new int[n];
    System.out.println("Enter Array nums");
    for(int i=0;i<n;i++){
        x[i]= input.nextInt(); 
    }
}
9
  • What exactly doesn't work? You code seems to be correct. Commented Apr 14, 2016 at 15:23
  • So, what is the problem? Commented Apr 14, 2016 at 15:23
  • But am troubled at the last part ... that tells us so good as nothing... what exactly are you getting and what are you expecting....??? Commented Apr 14, 2016 at 15:23
  • sorry, i have ordered everything but don't know how to actually add the nums Commented Apr 14, 2016 at 15:25
  • Use + operator or += operator. Commented Apr 14, 2016 at 15:25

3 Answers 3

2

You just need to initial variable with 0 which will have a sum of all values and then while taking a input the values are added into the variable initialized for holding the total values in the same for loop. Given below is the code for the same.

public static void main(String args[]){

     Scanner input= new Scanner(System.in);
       System.out.println("Enter size of array");
        int n= input.nextInt();
        int[] x= new int[n];
        System.out.println("Enter Array nums");
        int total=0;
        for(int i=0;i<n;i++){
            x[i]= input.nextInt(); 
            total=total+x[i];
        }
        System.out.println("total"+total);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Why not just write some code to add numbers?

import java.util.Scanner;
class X {
    public static void main (String[] args) {
        Scanner input= new Scanner(System.in);
        System.out.println("Enter size of array");
        int n= input.nextInt();
        int[] x= new int[n];
        System.out.println("Enter Array nums");
        for(int i=0;i<n;i++){
            x[i]= input.nextInt();
        }
        int sum = 0;
        for(int i=0;i<n;i++){
            sum+= x[i];
        }
        // to print the result, uncomment the line below
        //System.out.println(sum);
    }
}

7 Comments

why don't you do += in the first for?
@AndrewTobilko Because There are no need of adding numbers other than 1 (in i++) in the first for.
@AndrewTobilko Thank You very much. That was exactly what I couldn't understand how to do. Never realized how simple the code was, (I overthought it). :)
sum += (x[i] = input.nextInt()) looks prettier than yours
@AndrewTobilko Then, why do you need the array... because the question says "using an array"? Let's throw away x and just use sum += input.nextInt();
|
1

here is a method that will add up your array for you:

public int totalArray(int[] someArray) {
    int reply = 0;
    for (int value : someArray) reply += value;
    return reply;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.