- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the sum of all the numbers in an array in java?
You can find the sum of all the elements of an array using loops. Create a count variable with initial value 0, iterate the loop till end of the array, add each element to the count.
Example
import java.util.Arrays; import java.util.Scanner; public class SumOfAllNumbers { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int count = 0; System.out.println("Enter the size of the array that is to be created:: "); int size = sc.nextInt(); int[] myArray = new int[size]; System.out.println("Enter the elements of the array ::"); for(int i=0; i<size; i++) { myArray[i] = sc.nextInt(); count = count+myArray[i]; } System.out.println("Array is :: "+Arrays.toString(myArray)); System.out.println("Sum of all numbers in the array is :: "+count); } }
Output
Enter the size of the array that is to be created:: 5 Enter the elements of the array :: 45 56 85 78 99 Array is :: [45, 56, 85, 78, 99] Sum of all numbers in the array is :: 363
- Related Articles
- How do you find the sum of all the numbers in a java array
- Sum of all prime numbers in an array - JavaScript
- How to find the sum of all array elements in R?
- How to find all leaders in an array in Java?
- How to find the Odd and Even numbers in an Array in java?
- Java program to find the sum of elements of an array
- How to find the sum of all elements of a given array in JavaScript?
- Find All Numbers Disappeared in an Array in C++
- How to get the sum of the powers of all numbers in JavaScript?
- Find the sum of all the numbers from 1 to 200.
- Product of all the Composite Numbers in an array in C++
- How to find the sum of elements of an Array using STL in C++?
- How to Find a Cumulative Sum Array in Java?
- How to find all pairs of elements in Java array whose sum is equal to a given number?
- How to Find the Largest Palindrome in an Array in Java?

Advertisements