Java Online Compiler
Example: Sum of Array Elements using Traditional For Loop in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Sum of Array Elements using Traditional For Loop import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Define the array of integers int[] numbers = {10, 20, 30, 40, 50}; // Step 2: Initialize a variable to store the sum int sum = 0; // Step 3: Iterate through the array using a traditional for loop for (int i = 0; i < numbers.length; i++) { sum += numbers[i]; // Add each element to the sum } // Step 4: Print the calculated sum System.out.println("Sum of array elements (For Loop): " + sum); } }
Output
Clear
ADVERTISEMENTS