Java Online Compiler
Example: Sum of Squares using Traditional Loop in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Sum of Squares using Traditional Loop public class Main { public static void main(String[] args) { // Step 1: Define the input array int[] numbers = {1, 2, 3, 4}; // Step 2: Initialize a variable to store the sum of squares int sumOfSquares = 0; // Step 3: Iterate through each element of the array for (int i = 0; i < numbers.length; i++) { // Step 4: Square the current element and add it to the sum sumOfSquares += numbers[i] * numbers[i]; } // Step 5: Print the result System.out.println("Array: " + java.util.Arrays.toString(numbers)); System.out.println("Sum of squares (Traditional Loop): " + sumOfSquares); } }
Output
Clear
ADVERTISEMENTS