Java Online Compiler
Example: SumArrayTraditionalLoop in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// SumArrayTraditionalLoop public class Main { public static void main(String[] args) { // Step 1: Define the array of numbers 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 and add each element to the sum for (int number : numbers) { sum += number; } // Step 4: Print the calculated sum System.out.println("Sum using traditional loop: " + sum); } }
Output
Clear
ADVERTISEMENTS