Write a Java program that calculates the sum of the squares of integers using varargs
The class contains a method called sumOfSquares, which takes a variable number of integers as input. Inside this method, it iterates through each number in the input array and calculates the square of each number (multiplying it by itself). It then adds these squared values together to compute the sum of squares.
The main method is the entry point of the program. Inside main, it calls the sumOfSquares method with the numbers 1, 2, 3, 4, and 5 as arguments. It stores the result, which is the sum of squares, in the variable squareSum.
Finally, it prints out the result using System.out.println, displaying "Sum of Squares : " followed by the calculated sum of squares.
public class SquareSumCalculator { static int sumOfSquares(int... numbers) { int sum = 0; for (int num : numbers) { sum += num * num; } return sum; } public static void main(String[] args) { int squareSum = sumOfSquares(1, 2, 3, 4, 5); System.out.println("Sum of Squares : " + squareSum); } }
Sum of Squares : 55
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions