Java Online Compiler
Example: Java Program to Find Sum of N Numbers using For loop
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// 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); } }
8 44 42 34 23 42 34 23 56
Output
Clear
ADVERTISEMENTS