Write Java Source code: How to Add Numbers inside an Array using For Loop
Sample Output:
Enter the size of the input you want to enter: 5
Enter 5 numbers: 2 5 3 7 25
The sum of the numbers is:42
Source Code:
//java class
public
class
ArraySum
{
public
int
sumOfArray(int[] array)
{
int
sum = 0;
for(int
i=0; i<array.length; i++)
{
sum
= sum + array[i];
}
return
sum;
}
}
//main class
import
java.util.Scanner;
public
class
Main
{
public
static
void
main(String[]
args)
{
Scanner
input = new
Scanner(System.in);
System.out.print("Enter
the size of the input you want to enter: ");
int
size = input.nextInt();
int[]
numArr = new
int[size];
System.out.print("Enter
"+ size +" numbers: ");
for(int
i=0; i<numArr.length; i++)
{
numArr[i]=input.nextInt();
}
ArraySum
access = new
ArraySum();
System.out.print("The
sum of the numbers is:"
+
access.sumOfArray(numArr));
}
}