Java Program to Find Sum of N Numbers using For loop
ADVERTISEMENTS
Java program to find sum of N numbers using for loop. In this article, you will learn how to make java program to find sum of N numbers using for loop.
Source Code
// Java Program to Find Sum of N Numbers using For loop
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int s, i, sum = 0;
// s - To store the size of the array
System.out.println ("----Enter the size of the array----\n");
s = in.nextInt();
int x[];
x = new int[s];
// x - To store the array element to store the input numbers
System.out.println ("\n----The sum of the " + s + " input numbers----\n");
for(i = 0; i < s; i++) {
x[i] = in.nextInt();
sum += x[i];
}
System.out.println ("\n\nThe total sum is: " + sum);
}
}
Output
----Enter the size of the array----
8
----The sum of the 8 input numbers----
44
42
34
23
42
34
23
56
The total sum is: 298